chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Digging into my first few mocks I wanted to get some feedback as to if this is the correct approach. I am uneasy not being able to throw in an Assert to check that the Filter property was set so I wanted to be sure this is the correct approach. My HttpModule is quite simple at this point:

public class CombineScriptsModule : System.Web.IHttpModule
{
    #region Methods
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(Application_BeginRequest);
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        HttpRequest request = application.Request;
        HttpResponse response = application.Response;

        // Attach filter only to HTTP GETs on ASP.NET pages (.aspx)
        if ((string.Compare(request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase) == 0) &&
            (request.AppRelativeCurrentExecutionFilePath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase)))
        {
            response.Filter = new CombineScriptsFilter(response);
        }
    }
    #endregion
}


My first three tests are as follows:

[TestMethod, VerifyMocks]
public void VerifyThatEventsAreWiredUp()
{
    CombineScriptsModule target = new CombineScriptsModule();

    MockObject<HttpApplication> applicationMock = MockManager.MockObject<HttpApplication>();
    MockedEvent beginRequestHandler = applicationMock.ExpectAddEvent("BeginRequest");

    target.Init(applicationMock.Object);

    Assert.IsNotNull(beginRequestHandler.Instance);
}

[TestMethod, VerifyMocks]
public void AttachFilterToHttpGetRequests()
{
    CombineScriptsModule target = new CombineScriptsModule();

    MockObject<HttpApplication> applicationMock = MockManager.MockObject<HttpApplication>();
    MockedEvent beginRequestHandler = applicationMock.ExpectAddEvent("BeginRequest");

    MockObject<HttpRequest> httpRequestMock = MockManager.MockObject<HttpRequest>();
    applicationMock.ExpectGetAlways("Request", httpRequestMock.Object);

    MockObject<HttpResponse> httpResponseMock = MockManager.MockObject<HttpResponse>();
    applicationMock.ExpectGetAlways("Response", httpResponseMock.Object);

    httpRequestMock.ExpectGetAlways("HttpMethod", "GET");
    httpRequestMock.ExpectGetAlways("AppRelativeCurrentExecutionFilePath", "foobar.aspx");

    httpResponseMock.ExpectGetAlways("UsingHttpWriter", true);
    httpResponseMock.ExpectSet("Filter").Args(Check.IsTypeOf<CombineScriptsFilter>());

    target.Init(applicationMock.Object);
    beginRequestHandler.Fire(applicationMock.Object, EventArgs.Empty);
}

[TestMethod, VerifyMocks]
public void IgnoresPostbackResponses()
{
    CombineScriptsModule target = new CombineScriptsModule();

    MockObject<HttpApplication> applicationMock = MockManager.MockObject<HttpApplication>();
    MockedEvent beginRequestHandler = applicationMock.ExpectAddEvent("BeginRequest");

    MockObject<HttpRequest> httpRequestMock = MockManager.MockObject<HttpRequest>();
    applicationMock.ExpectGetAlways("Request", httpRequestMock.Object);

    MockObject<HttpResponse> httpResponseMock = MockManager.MockObject<HttpResponse>();
    applicationMock.ExpectGetAlways("Response", httpResponseMock.Object);

    httpRequestMock.ExpectGetAlways("HttpMethod", "POST");
    httpRequestMock.ExpectGetAlways("AppRelativeCurrentExecutionFilePath", "foobar.aspx");

    target.Init(applicationMock.Object);
    beginRequestHandler.Fire(applicationMock.Object, EventArgs.Empty);
}


Does this make sense? How would you approach this?

Thanks!
Colin
asked by cbowern (640 points)

3 Answers

0 votes
Hi

I think that the test VerifyThatEventsAreWiredUp
is redundant. The line
Assert.IsNotNull(beginRequestHandler.Instance);

doesn't really check something since beginRequestHandler.Instance
will not be null when you create the MockedEvent.
In other words the following code will also pass:
[Test, VerifyMocks]
public void VerifyThatEventsAreWiredUp()
{
    CombineScriptsModule target = new CombineScriptsModule();

    MockObject<HttpApplication> applicationMock = MockManager.MockObject<HttpApplication>();
    MockedEvent beginRequestHandler = applicationMock.ExpectAddEvent("BeginRequest");
    Assert.IsNotNull(beginRequestHandler.Instance);
}


If you want to check that the event is wired I suggest to fire a mocked event and than check that your event handler is called.

[Test, VerifyMocks]
public void VerifyThatEventsAreWiredUp()
{
    MockObject<HttpApplication> mockHttpApplication = MockManager.MockObject<HttpApplication>();
    MockedEvent mockedEvent = mockHttpApplication.ExpectAddEvent("BeginRequest");

    Mock mockCombineScriptsModule = MockManager.Mock<CombineScriptsModule>();
    mockCombineScriptsModule.ExpectCall("Application_BeginRequest");

    CombineScriptsModule combineScriptsModule = new CombineScriptsModule();
    combineScriptsModule.Init(mockHttpApplication.Object);

    mockedEvent.Fire(mockHttpApplication.Object, EventArgs.Empty);
}


As for the AttachFilterToHttpGetRequests I think there should be a way to
Assert that applicationMock.Object.Response.Filter is set.
I'll get back to you later with an answer.
answered by ohad (35.4k points)
0 votes
As for the AttachFilterToHttpGetRequests I think there should be a way to Assert that applicationMock.Object.Response.Filter is set.


Thanks Ohad! Any thoughts on testing the Filter property would be great. As of now I'm using something like this:

httpResponseMock.ExpectSet("Filter").Args(Check.And(Check.NotNull(), Check.IsTypeOf<CombineScriptsFilter>()));
answered by cbowern (640 points)
0 votes
Hi
Oops sorry :oops: my bad.
Actually the way you did it is the way to go here
Expect set and than use the VerifyMocks attribute.

The thing here is if you use ExpectSet the value of Filter will be null
(you can't assert).
You can use Mock.ExpectUnmockedSet and than you will get the real value in Filter but I'm not sure if it's a good idea to mock only part of the HttpResponse class.
answered by ohad (35.4k points)
...