用RhinoMocks模拟密封类

本文关键字:密封类 模拟 RhinoMocks | 更新日期: 2023-09-27 17:50:37

我对TDD相当陌生,我试图在MVC应用程序中模拟HttpContextBase。我还需要模拟响应属性和HttpCookieCollection。

HttpCookieCollection类是密封的,并且RhinoMocks说它不能模拟密封类。

有什么建议吗?

我的测试如下:

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

用RhinoMocks模拟密封类

我会说嘲弄HttpCookieCollection有点过头了-它只是一种存储cookie的方式-您不会嘲笑IList<Cookie>,不是吗?

直接做

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

或类似的(没有使用Rhino mock,所以不确定这是否完全正确)。