Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:40:19+00:00 2026-05-30T17:40:19+00:00

For a unit test I use moq to mock the HttpServerUtilityBase attached to a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:40:21+00:00Added an answer on May 30, 2026 at 5:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I've begun to use Moq to unit test. I use Moq to mock
How do I write a unit test for a method that use an external
If unit-test names can become outdated over time and if you consider that the
Unit test is in Java, using Maven to build & run. I can use
Can I use VS 2010 Profiler to profile unit test (MSTest)? If yes, how?
I'm trying to wtire unit tests for a function that looks something like this:
I'm trying to use Test::More to unit test Perl functions that print to the
It came to my attention lately that you can unit test abstract base classes
I use the Boost Test framework to unit test my C++ code and wondered
I need to use the ReSharper Unit Test Runner to run my MSTest Unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.