How to trick an ASP.NET MVC membership class to enable testing

I am creating an ASP.NET MVC 3 site that allows users to register. For this purpose I use the built-in (static) class Membership ; some say itโ€™s bloated, but it works well and is easy enough, so you are there.

In any case, I started writing the AdminService class, which will work with functions related to the user account, and suppose that it has only one method:

 public class AdminService : IAdminService { public void DeleteUser(string username) { Membership.DeleteUser(username); } } 

Using the Membership class as it is wrong in two ways: it cannot be entered through IoC, and it is increasingly difficult for me to write specifications (test) for IAdminService , because I can not scoff at the Membership class.

Is there a way to make the ASP.NET membership class test-friendly and friendly without quotes?

When it comes to functionality, the Membership class works well and, more importantly, it works now, so I'm really reluctant to start writing my own MemberhipProvider, as that will only slow me down.

+4
source share
2 answers

When you create a non-empty ASP.NET MVC 3 project, the AccountModels.cs file will be created in the Models folder. It contains an example of using membership for unit testing:

 public interface IMembershipService { int MinPasswordLength { get; } bool ValidateUser(string userName, string password); MembershipCreateStatus CreateUser(string userName, string password, string email); bool ChangePassword(string userName, string oldPassword, string newPassword); } 
+3
source

There was the same situation when I built the MVC website, and eventually switched to my own (verified) provider.

This means that one option would be to simply raise the sample implementation from here - you could (theoretically) just create a FakeProvider, and hard code the various return methods and delete the SQL database.

The total number of methods in one of them is quite bright, and it would be nice to deal with when you want to fake them.

0
source

All Articles