To install, in the package manager console, type

Install-package compdj.web.abstractions

for test project, type

Install-package compdj.web.abstractions.tests


I know I have spent a lot of time trying to unit test existing ASP.NET MVC applications. We all seem to write similar code for testing. Not anymore.

Web.Abstractions allows you to unit test your web code, and abstract the

  • HttpContext
  • FormsAuthentication
  • Membership
  • ConfigurationManager


Currently the test helper uses FakeItEasy but it can be adopted for any mocking framework.

 It allows you to create tests with mocking like this:

[TestMethod]
public void MyTestMethod()
{
    //// Resets all the factories (must be done before each test)
    WebAbstractions.Factory.ResetAllFactories();
    //// Initialize all the mocking
    WebAbstractions.Factory.InitializeMock(f =>
    {
        //// Mocking for IFormsAuthentication here
        A.CallTo(() => f.SetAuthCookie(A<string>.Ignored, A<bool>.Ignored)).
            DoesNothing();
    }, m =>
    {
        //// Mocking for IMembership here
        A.CallTo(() => m.ValidateUser(A<string>.Ignored, A<string>.Ignored)).
            Returns(true);
    }, c =>
    {
        //// Mocking for IConfigurationManager Here
        A.CallTo(() => c.GetSection(A<string>.Ignored)).Returns("Unit test");
    }, h =>
    {
        //// Mocking for HttpContextBase Here
        A.CallTo(() => h.Session.SessionID).Returns("1234567890");
    });

    var target = new HomeController();
    var result = target.Home();
    
    Assert.IsNotNull(result);
}

See the documentation page for more info. 

Last edited May 28, 2013 at 12:00 AM by rickrat, version 22