Dave Amour
Dave Amour

Reputation: 165

Mocking Sealed Class with RhinoMocks

I am fairly new to TDD and I am trying to mock HttpContextBase in an MVC app. I also need to mock the Response property and the HttpCookieCollection of that.

The HttpCookieCollection class is sealed though and RhinoMocks says it cannot mock sealed classes.

Any advice on how I should tackle this.

My test is below:

    [TestMethod]
    public void CreateSignInTicketCreateTempCookie()
    {
        const string email = "[email protected]";

        var mockHttpContextBase = MockRepository.GenerateMock<HttpContextBase>();
        var response = MockRepository.GenerateMock<HttpResponseBase>();

        var mockUserRepository = MockRepository.GenerateStub<IUserRepository>();
        var cookieCollection = MockRepository.GenerateStub<HttpCookieCollection>();

        mockHttpContextBase.Stub(x => x.Response).Return(response);

        response.Stub(x => x.Cookies).Return(cookieCollection);

        var webAuth = new WebAuthenticator(mockUserRepository);

        webAuth.CreateSignInTicket(mockHttpContextBase, email);

        Assert.IsTrue(mockHttpContextBase.Response.Cookies.Count == 1);
    }

Upvotes: 0

Views: 1465

Answers (1)

dav_i
dav_i

Reputation: 28107

I would say mocking HttpCookieCollection is taking things a bit too far - it's just a way of storing cookies - you wouldn't mock an IList<Cookie>, would you?

Simply do

response.Stub(x => x.Cookies).Return(new HttpCookieCollection());

or similar (not used Rhino Mocks so not sure if this is exactly right).

Upvotes: 2

Related Questions