1

For a unit test I use moq to mock the HttpServerUtilityBase attached to a HttpContextBase. This is so that when the function under test can call context.server (ASP.Net).

However, when the tested method does context.server.transfer, when done in the application this causes the function (called within a httphandler) to stop execution. In my moq'd version, the "transfer" call is performed and then execution continues.

To combat this, I've put return statements after the transfer but these are correctly identified as redundant by stylecop.

Can I set up my Mock to force the calling method to return?

Eg if we're testing a method like this:

public void ProcessRequest(HttpContextBase context, string username, Logger logger)
{
    if (string.isNullOrEmpty(username))
    {
        logger.Warn("Blah Blah invalid username");
        context.Server.Transfer("~/ErrorPage.aspx");
    }
    ETC...
}

The following code will correctly test that the right action is taken on an invalid username.

[TestMethod]
public void ProcessRequestTest()
{
    // Set up my mocks
    var logger = new Mock<ILogger>();
    logger.Setup(l => l.Warn(It.IsAny<string>());
    var context = new Mock<HttpContextBase>();
    var server = new Mock<HttpServerUtilityBase>();
    context.Setup(c => c.Server).Returns(server.Object);

    // Try an invalid call
    var rh = new MyRequestHandler();
    rh.ProcessRequest(context.Object, string.empty, logger.Object);

    // Check that the message was logged and the transfer was made
    logger.Verify(l => l.Warn(It.IsRegex("invalid username"));
    server.Verify(s => s.Transfer(It.IsAny<string>()));
}

However because the context.Server is mocked, the Transfer will not stop execution of ProcessRequest, and unless I put in a return after the transfer the test will execute past the "ETC..." block.

Richard Craggs
  • 155
  • 1
  • 2
  • 7

1 Answers1

1

This is pretty tricky and is why it is not usually recommended to mock objects that you don't own. One solution would be to cast an exception (which I think is how it is done in the ASP.NET code). See this link for an example under the section "Stopping execution after the redirect".

So something like:

server.Setup(s => s.Transfer(It.IsAny<string>()).Throws<TransferException>();

Then catch that with NUnit's Assert.Throws.

Daniel Lee
  • 7,709
  • 2
  • 48
  • 57
  • This is a good answer. At first out seemed a bit of a hack to throw an exception to stop execution, but the link explains that this is what the framework does on a transfer – Richard Craggs Mar 02 '12 at 20:00