I am currently using Moq to write automated tests for System.Web.HttpApplication in C#:

public FakeHttpApplication CreateBaseMocks()
    MockContext = new Mock<HttpContextBase>();
    MockRequest = new Mock<HttpRequestBase>();
    MockResponse = new Mock<HttpResponseBase>();
    MockSession = new Mock<HttpSessionStateBase>();
    MockServer = new Mock<HttpServerUtilityBase>();

    MockContext.Setup(ctx => ctx.Request).Returns(MockRequest.Object);
    MockContext.Setup(ctx => ctx.Response).Returns(MockResponse.Object);
    MockContext.Setup(ctx => ctx.Session).Returns(MockSession.Object);
    MockContext.Setup(ctx => ctx.Server).Returns(MockServer.Object);

    HttpContextBase = MockContext.Object;
    HttpRequestBase = MockRequest.Object;
    HttpResponseBase = MockResponse.Object;

    MockRequest.Setup(r => r.Headers).Returns(new NameValueCollection());
    return this;

public FakeHttpApplication SetupNormalRequestValues()
    MockUser = new Mock<IPrincipal>();
    MockIdentity = new Mock<IIdentity>();
    MockContext.Setup(context => context.User).Returns(MockUser.Object);
    MockUser.Setup(context => context.Identity).Returns(MockIdentity.Object);

    MockRequest.Setup(request => request.InputStream).Returns(new MemoryStream());

    MockResponse.Setup(response => response.OutputStream).Returns(new MemoryStream());
    return this;

However, setting up the objects this way leaves a lot to be desired considering that most of the internal objects and properties remain null. Does anyone know of a better way to do this? I am looking for a way to instantiate a more lifelike HttpApplication object and don't mind a little complexity.

  • Are you asking because you expect code that you don't own to use this mock object as if it were a real HttpApplication? That does sound difficult. Perhaps you could somehow get/create an appropriate real HttpApplication, then put a test wrapper around it - intercept what you need to, and pass everything else on to the real object. – Bruce May 4 '11 at 6:36
  • I wonder if perhaps this would be better suited to stackoverflow as its primarily a programming question, albeit with an application in testing. – Rob May 5 '11 at 13:43
  • After looking at this code and other examples a little more, I think this question can be generalized. What I'm really looking for is an example of what the best way to write automated tests for a standard ASP.NET 4.0 application. I do not have the benefit of using MVC. – Drew May 10 '11 at 19:21
  • I gave an answer, but would be interested in specifically what you are trying to test / abstract away. Are you trying to test the back-end code without having to think about UI? I generally work with devs who stick to thin clients and use services on the backend, so I just call their APIs for as many of my tests as I can, then do additional testing on the UI and E2E scenarios to get the rest of the coverage I need. I'm not sure how this compares to what you are dealing with. – Ethel Evans May 13 '11 at 21:46
  • I am testing an HttpModule with State Verification to determine if given conditions are true or false at the end of the request. My module is a component that does image resizing and remote file downloads & caching, so I just need to simulate Http requests. My challenge is I want the code coverage in TeamCity to work so that the tests can show a Red/Green. I am looking for the best way to do this without using a UI. I don't think Selenium can be automated with TeamCity this way. – Drew Jun 6 '11 at 18:44

I am trying a mock that uses an Object List of return values as a queue. Every time my mock is supposed to return something, it first looks in the queue to see if I've queued anything, and if so, returns it.

Then I implemented an additional Queue method for my mock that I can call during test setup that just adds items to the List in the order I want them returned.

In this case, all of my return values will be ActionResults (we are using MVC) so I don't need to worry about return types, but you could use Reflection to implement something similar, where you return the first item in the queue that IsAssignableTo the return type of the method (I think that's the method in Reflection that you want . . . not sure).

You could also look at other mocking libraries that have this kind of "Record and Playback" functionality built-in. Moq is geared more towards being able to see if the right APIs are being called, etc., then about checking what the mocked interface returns. This article digresses into a discussion of philosophies about testing with mocks that explains this difference.

  • 1
    I am not using MVC, this is an HttpModule. My challenge is I want the code coverage in TeamCity to work so that the tests can show a Red/Green. I am looking for the best way to do this without using a UI. I don't think Selenium can be automated with TeamCity this way. – Drew Jun 6 '11 at 18:48
  • I'm not talking about Selenium or UI testing. "Record and Playback" with mocks means queuing values to return (recording), and then when your test calls those methods, they get returned (playback). Rhino Mocks is an example of this. See ayende.com/wiki/Rhino+Mocks+Introduction.ashx for examples of this style of mocking. Incidentally, while writing this comment I learned that Rhino Mocks has moved beyond Record and Playback to Arrange, Act, and Assert: blog.benhall.me.uk/2008/05/… – Ethel Evans Jun 6 '11 at 22:31

I would probably use selenium to drive the UI testing in the browser for ASP.NET. The UI test will be more brittle but they will give you an accurate indication if you application works correctly.

You will find that mocking the items you have shown will still need you to test that they function correctly through the UI. So you might as well push the testing up to integrated UI testing and drive them through a browser.

  • My challenge is I want the code coverage in TeamCity to work so that the tests can show a Red/Green. I am looking for the best way to do this without using a UI, can Selenium be automated with TeamCity to run this way? – Drew Jun 6 '11 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.