Testing a custom user profile for use with ASP Membership
I've just finished implementing a basic asp membership into my MVC project and I'm looking to write some tests. I had some initial problems with mocking the provider but now that is sorted. Obviously as the provider is mocked no data is in there nor can you add any data but the profile's cannot be directly mocked as they are created every time a user profile is required. I'm not sure how to approach testing these as (obviously) when a profile is called it returns null.
For example:
Using DI my repository is injected:
public AccountMembershipService(MembershipProvider provider)
{
_provider =开发者_如何学C provider ?? Membership.Provider;
}
And lets take for example, the create user method
public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer)
{
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, passwordQuestion, passwordAnswer, true, null, out status);
if (status == MembershipCreateStatus.Success)
{
UserProfile profile = UserProfile.GetProfile(userName);
profile.IPAddress = "0.0.0.0";
profile.Save();
}
return status;
}
The .CreateUser works perfectly (As far as mocking is concerned) but when it comes to GetProfile obviously there is no profile in existence so it just errors. Any ideas on how I might be able to approach testing this?
I've been staring at this for a good couple of hours now, any help would be REALLY appreciated!!
Have a look at using Pex Moles to replace Create method of the Base UserProfile class.
Another option could be to create a service object that makes the call to GetProfile(), this could then be abstracted and a concrete class injected to the AccountMembershipService at runtime, or passed to the constructor during unit tests.
精彩评论