lundi 25 mai 2015

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

Aucun commentaire:

Enregistrer un commentaire