Search code examples
c#asp.net-mvc-3unit-testingrhino-mocks

Problems when mocking HttpResponeBase and HttpRequestBase


I got a mvc 3 project where I want to mock HttpResonseBase and HttpRequestBase. Im using RhinoMocks 3.6 to mock myobjects. My test code rightnow looks like this.

[TestMethod]
public void Test()
{
    MockRepository repo = new MockRepositoy();
    HttpContextBase mockHttpContext= repo.StrictMock<HttpContextBase>();
    HttpRequestBase mockRequest = repo.StrictMock<HttpRequestBase>();
    HttpResponseBase mockResponse = repo.StrictMock<HttpResponseBase>();
    ICookie mockCookie = repo.StrictMock<ICookie>();
    Controller instanceToTest = new Controller(mockCookie);

    SetupResult.For(mockHttpContext.Request).Return(mockRequest);
    SetupResult.For(mockHttpContext.Response).Return(mockResponse);

    mocks.Replay(context);

    instanceToTest.ControllerContext = new ControllerContext(mockHttpContext, new RouteData(), instanceToTest);

    mockCookie.Expect(x=>x.MethodToExpect("Test",mockRequest,mockResponse);


    mockRepository.ReplayAll();
    instanceToTest.MethodToTest();
    mockRepository.VerifyAll();
}

When im running the test I get this errormessage;

Rhino.Mocks.Exceptions.ExpectationViolationException: ICookie.MethodToExpect("Test", System.Web.HttpResponseBase, System.Web.HttpRequestBase); Expected #0, Actual #1.
ICookie.MethodToExpect("Test", HttpResponseBaseProxy); Expected #1, Actual #0.

What am I doing wrong?


Solution

  • The problem here is that you use StrictMock - it means that if you call a method on the Mock object that you haven't set any expectations on it, VerifyAllExpectations will fail. You could use MockRepository.GenerateMock<T> method instead of the StrictMock.
    Another comment is that you'd better stick with the RhinoMocks AAA syntax (using the Expect, Stub and VerifyAllExpectations methods instead of ReplayAll, SetupResult etc...)
    Here's how your code may look like with pure AAA syntax:

    [TestMethod]
    public void Test()
    {
        // Arrange(A) - create your objects, mocks and stubs
        // The context is a Stub - you just want it to return the mocked request and response
        HttpContextBase mockHttpContext= MockRepository.GenerateStub<HttpContextBase>();
        HttpRequestBase mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
        HttpResponseBase mockResponse = MockRepository.GenerateMock<HttpResponseBase>();
        ICookie mockCookie = MockRepository.GenerateMock<ICookie>();
        Controller instanceToTest = new Controller(mockCookie);
    
        // Stub will return the mocked request and response on every call (similar to SetupResult)
        mockHttpContext.Stub(x => x.Request).Return(mockRequest);
        mockHttpContext.Stub(x => x.Response).Return(mockResponse);
    
        instanceToTest.ControllerContext = new ControllerContext(mockHttpContext, new RouteData(), instanceToTest);
    
        mockCookie.Expect(x=>x.MethodToExpect("Test",mockRequest,mockResponse);
    
        // Act(A) - do the actual operations on the tested class
        instanceToTest.MethodToTest();
    
        // Assert (A) - Verify your expectations
        mockCookie.VerifyAllExpectations();
    }