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

  • SEARCH
  • Home
  • 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 6657529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:46:46+00:00 2026-05-26T01:46:46+00:00

I got a mvc 3 project where I want to mock HttpResonseBase and HttpRequestBase.

  • 0

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?

  • 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-26T01:46:46+00:00Added an answer on May 26, 2026 at 1:46 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a spring mvc project by maven, I got the following
I've got menu in my ASP MVC project, and can when I use <li><%=
I've got an issue with an ASP.Net MVC project in that on most of
I got a ASP.NET MVC controller like this [Authorize] public class ObjectController : Controller
I've got two classes in my MVC project Order and Product . Because Product
I've currently got a ASP.NET MVC 2 application on .NET 3.5 and I want
i've got a situtation where we have a web site (mvc project), and a
I'm on my first MVC project and still haven't got a complete hang of
I've added code to implement a RestTemplate Client to my Spring MVC project (Eclipse,
We've got an ASP.NET MVC 3 project that uses single action controllers , and

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.