This project is read-only.
Project Description
Many components of the System.Web.Security namespace are static methods that are difficult to mock or stub for testing. MockableWebSecurity wraps many of these classes in interfaces with proxy implementations allowing for more easy creation of testable code.

For example, in an ASP.NET MVC Controller you might wish to create a membership user. Typically this would be done by calling the static membership class like so:

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
}

This is all fine and dandy until you try and write unit tests for this test. It is unlikely that the membership infrastructure will be available in unit tests. In any case, you wouldn't want to be attempting to create actual users in your unit tests. Using MockableWebSecurity, you can inject an IMembership instance into your controller and perform your user creation via this interface:

public class AccountController
{
    private IMembership _membership;

    public AccountController(IMembership membership)
    {
        _membership = membership;
    }

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        _membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
    }
}

Using this method, you can verify in your unit tests that the Register method creates a membership user using your favourite unit testing and mocking framework (such as Rhino Mocks or Moq).

Last edited Aug 31, 2011 at 11:24 PM by apjanes, version 4