Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock the Response.StatusCode with Moq?

I have the following method:

    public void SetHttpStatusCode(HttpStatusCode httpStatusCode)
    {
        Response.StatusCode = (int)httpStatusCode;
    }

And the following test:

    [TestMethod]
    public void SetHttpStatusCode_SetsCorrectStatusCode()
    {
        //Arrange
        //Any url will suffice
        var mockHttpContext = TestHelpers.MakeHttpContext(""); 
        mockHttpContext.SetupSet(x => x.Response.StatusCode = It.IsAny<int>());

        //creates an instance of an asp.net mvc controller
        var controller = new AppController()
        {
         ControllerContext = new ControllerContext() { 
                   HttpContext = mockHttpContext.Object }
                             };

        // Act
        controller.SetHttpStatusCode(HttpStatusCode.OK);

        //Assert
        mockHttpContext.VerifySet(x => x.Response.StatusCode = It.IsAny<int>());
    }

Also, Here is MakeHttpContext

 public static Mock<HttpContextBase> MakeHttpContext(string url)
    {
        var mockHttpContext = new Mock<HttpContextBase>();
        var mockRequest = new Mock<HttpRequestBase>();
        var mockResponse = new Mock<HttpResponseBase>();
        var mockSession = new Mock<HttpSessionStateBase>();

        //request
        mockRequest.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(url);
        mockHttpContext.Setup(x => x.Request).Returns(mockRequest.Object);

        //response
        mockResponse.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => x);
        mockHttpContext.Setup(x => x.Response).Returns(mockResponse.Object);

        //session
        mockHttpContext.Setup(x => x.Session).Returns(mockSession.Object);

        return mockHttpContext;
    }

When I run the test, I get the following exception:

    Test method PA.Tests.Controllers.AppControllerTest.SetHttpStatusCode_SetsCorrectStatusCode
threw exception: 


      Moq.MockException: 
        Expected invocation on the mock at least once, 
        but was never performed: x => x.StatusCode = It.IsAny<Int32>()

        Configured setups:
        x => x.StatusCode = It.IsAny<Int32>(), Times.Never
        No invocations performed.

How does Moq expect/require invocations to be called? I've debugged the SetHTTPStatusCode method, the response object is indeed a mocked object, however Moq insists that there was no invocation. Am I missing something?

Thanks!

like image 758
Kamau Malone Avatar asked May 25 '12 14:05

Kamau Malone


People also ask

How do you mock a method in Moq?

First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.

What can be mocked with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces.

What is Moq mocking framework?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.


1 Answers

You haven't shown what your TestHelpers.MakeHttpContext method does so it's a bit difficult to understand what's going on.

Try like this:

// Arrange
var mockHttpContext = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
mockHttpContext.SetupGet(x => x.Response).Returns(response.Object);

//creates an instance of an asp.net mvc controller
var controller = new AppController()
{
    ControllerContext = new ControllerContext() 
    { 
        HttpContext = mockHttpContext.Object 
    }
};

// Act
controller.SetHttpStatusCode(HttpStatusCode.OK);

//Assert
response.VerifySet(x => x.StatusCode = (int)HttpStatusCode.OK);
like image 160
Darin Dimitrov Avatar answered Oct 29 '22 01:10

Darin Dimitrov