ASP.NET - Unit test MembershipProvider

I am trying to unit test MemberhipProvider, however I cannot figure out how or is there a need for unit testing it ...

My business level:

public interface IAccountService { MembershipCreateStatus CreateUser(string userName, string password, string email); } public class AccountService : IAccountService { private readonly MembershipProvider provider; public AccountService() : this(null) { } public AccountService(MembershipProvider providera) { this.provider = providera ?? Membership.Provider; } public MembershipCreateStatus CreateUser(string userName, string password, string email) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", userName); if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", password); if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", email); MembershipCreateStatus status; provider.CreateUser(userName, password, email, null, null, true, null, out status); return status; } } 

The only examples I have found so far require a "MockMembershipProvider" with local database setup ... it seems pretty strange to me.

Thanks in advance.

+6
c # unit-testing testing
source share
1 answer

Having a "MockMembershipProvider with local database setup" is odd for several reasons.

Usually you do not want a unit test data access code. Testing your device must be very fast and often and therefore does not require access to the database. This is why you should be able to make fun of your level of data access. Stored data will be acceptable for integration testing, but usually it is not unit testing.

The rest of this answer is based on the assumption that you do not want to hit DB in your unit test.


If you want a unit test, the membership provider will depend on what happens there.

  • If the membership provider is custom-written and contains business logic, then it must be verified by the department. If this is the case, you need to create a DAO object layout inside the membership provider so that the membership provider can be implemented with unit tests without getting into the database.

  • If the membership provider simply accesses the database (either directly or with call forwarding to the data access level) you should not unit test it. If you use the Microsoft asp.net membership provider, you should not test it either.

    Instead, you should create a mock MembershipProvider for use in the AccountService class. You will enter the layout using constructor injection, this is the goal of the following code template

     public AccountService() : this(null) { } public AccountService(MembershipProvider providera) { this.provider = providera ?? Membership.Provider; } 

    This code makes it easy to embed the constructor of alternative implementations (including layouts). An example of what a test might look like:

      [Test] public void ExampleTestWithAHandRolledMock() { //arrange var mockMembershipProvider = new MockMembershipProvider();//no db access in this mock implementation var accountService = new AccountService(mockMembershipProvider); //act accountService.CreateUser("foo", "bar", "baz"); //assert Assert.IsTrue(mockMembershipProvider.MockUserExists("foo","bar","baz");//added a method to mock to confirm user was added } 
+6
source share

All Articles