Testing Raven and ASP.NET Modules

I am starting a web application that will use asp.net membership services (with sql server backend) to keep track of users and RavenDb for everything else.

I am new to unit testing and would appreciate it if I could skip past what I still have with one example.

This is HelixManager

public class HelixManager:IDisposable { private readonly IMembershipProvider _membership; private readonly IRepository _repos; public HelixManager() { _membership = new AspNetMembershipProvider(); _repos = new RavenRepository(); } public HelixManager(IMembershipProvider membershipProvider, IRepository repos) { _membership = membershipProvider; _repos = repos; } public User CreateAdmin(User newUser, string password) { if (String.IsNullOrEmpty(newUser.Email)) throw new ArgumentException("Email must be supplied"); if (String.IsNullOrEmpty(password)) throw new ArgumentException("Password must be supplied"); var memberId = _membership.CreateUser(newUser, password); if (memberId != null) { _membership.AddToRole(newUser, "Admin"); newUser.Type = UserType.Admin; newUser.MemberId = memberId; _repos.Store<User>(newUser); } return newUser; } 

This is IMembershipProvider

 public interface IMembershipProvider { string CreateUser(User newUser, string password); void AddToRole(User user, string rolename); } 

and implementation of AspNetMembershipProvider

 public class AspNetMembershipProvider : IMembershipProvider { public string CreateUser(User newUser, string password) { MembershipCreateStatus status; MembershipUser memUser = System.Web.Security.Membership.CreateUser(newUser.Email, password, newUser.Email, "", "", true, out status); return memUser.ProviderUserKey.ToString(); } public void AddToRole(User user, string role) { Roles.AddUserToRole(user.Email, role); } } 

This is IRepository

 public interface IRepository { T Store<T>(T item); } 

and its implementation

 public class RavenRepository : IRepository { private readonly DocumentStore _store; public RavenRepository() { _store = new DocumentStore { DefaultDatabase = "Helix", Url = "http://localhost:8080" }; _store.Initialize(); } public T Store<T>(T item) { using (var session = _store.OpenSession()) { session.Store(item); session.SaveChanges(); } return item; } } 

In my test project, I created fake implementations of both of them:

FakeMembershipProvider:

 class FakeMembershipProvider : IMembershipProvider { public string CreateUser(User newUser, string password) { CreatedUser = true; return newUser.Email == " email@example.com " ? Guid.NewGuid().ToString() : null; } public void AddToRole(User user, string rolename) { AddedToRole = true; } public bool AddedToRole; public bool CreatedUser; } 

and FakeRepository:

 public class FakeRepository : IRepository { public T Store<T>(T item) { StoreCalled = true; return item; } public bool StoreCalled; } 

Testing is carried out as follows:

 public class UserManagementTests { private readonly HelixManager _hm; private readonly IMembershipProvider _fakeMembershipProvider; private readonly IRepository _fakeRepository; public UserManagementTests() { _fakeMembershipProvider = new FakeMembershipProvider(); _fakeRepository = new FakeRepository(); _hm = new HelixManager(_fakeMembershipProvider, _fakeRepository); } [TestMethod] public void CreateAdminReturnsValidAdminUser() { var newUser = new User { AvatarName = "fred", Email = " email@example.com ", Forename = "Fred", Surname = "Jones" }; _hm.CreateAdmin(newUser, "password"); Assert.IsNotNull(newUser.MemberId); Assert.AreEqual(UserType.Admin, newUser.Type); } 

What do I ask before I get a further path along this line, is this happening correctly? Or are there any better ways to do this?

I also plan to have an IntegrationTests project that will use real db and a real RavenDb instance to test end to end.

Greetings

Dave

+4
source share
3 answers

I do not see anything wrong with this approach. You can go the way of using the Mocking tool ( Rhino Mocks , Moq , NSubstitute ) to create your fakes, rather than doing it manually, but this is really a matter of personal preference and your personal level of comfort with the tools.

Your test is clearly structured, state-based (which is good), and you are not trying to check too many things at once, which is a common mistake (even for those of us who have been doing this for a while).

If you haven’t seen this yet, I can highly recommend Roy Osherova Art of Unit Testing , which has a lot of very good information about unit testing, including things like cleaning up tests, how to deal with less verifiable areas of code, etc.

I say hold the truck :)

+2
source

Your tests look completely normal to me. Right approach (mocks) to the right (AAA).

Forward:)

+2
source

My problem is actually with this:

I am starting a web application that will use asp.net membership services (with sql server) to look after users and RavenDb for everything else.

Why are you doing it? It would be easier to do everything in RavenDB, and your problem with testing modules would be as simple as the new EmbeddableDocumentStore {RunInMemory = true}

+2
source

All Articles