简体   繁体   English

用RhinoMocks模拟密封课程

[英]Mocking Sealed Class with RhinoMocks

I am fairly new to TDD and I am trying to mock HttpContextBase in an MVC app. 我对TDD相当陌生,并且正在尝试在MVC应用程序中模拟HttpContextBase。 I also need to mock the Response property and the HttpCookieCollection of that. 我还需要模拟Response属性和HttpCookieCollection。

The HttpCookieCollection class is sealed though and RhinoMocks says it cannot mock sealed classes. HttpCookieCollection类是密封的,而RhinoMocks说它不能模拟密封的类。

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? 我会说嘲笑HttpCookieCollection的工作有点过头了-这只是一种存储cookie的方式-您不会嘲笑IList<Cookie> ,对吧?

Simply do 简单地做

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

or similar (not used Rhino Mocks so not sure if this is exactly right). 或类似(未使用过的Rhino Mocks,因此不确定是否正确)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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