简体   繁体   中英

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 = "dave@somewhere.co.uk";

        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);
    }

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM