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.