I am performing some unit tests and want my MembershipProvider to initialize the dependencies. I run the following code before any test is executed [TestFixtureSetup].
public static void StructureMapConfiguration()
{
ObjectFactory.Initialize(InitializeUsingScanning);
}
private static void InitializeUsingScanning(IInitializationExpression obj)
{
obj.Scan(
x =>
{
x.Assembly("EStudyMongoDb.Business");
x.WithDefaultConventions();
}
);
}
Here is my EstablishContext method which is triggered before running any test:
public override void EstablishContext()
{
_provider = ObjectFactory.GetInstance<MongoDbMembershipProvider>();
_config.Add("applicationName", "EStudyApplication");
_config.Add("name", "EStudyMembershipProvider");
_config.Add("requiresQuestionAndAnswer", "false");
_provider.Initialize(_config["name"], _config);
}
Here is my test:
[TestFixture]
public class when_creating_a_new_user : specification_for_membership_provider
{
public override void When()
{
_user = _provider.CreateUser("johndoe", "password", "[email protected]", String.Empty, String.Empty,
true, null, out _status);
}
[Test]
public void should_create_successfully()
{
var vUser = Membership.GetUser(_user.UserName);
Assert.IsNotNull(vUser);
}
}
Now, in the Membership.GetUser method I try to access the _userRepository but I get null.
The MongoMembershipProvider constructor looks like the following:
public MongoDbMembershipProvider(IUserRepository userRepository, IRoleService roleService, IAuthenticationService authenticationService)
{
_userRepository = userRepository;
_roleService = roleService;
_authenticationService = authenticationService;
}
UPDATE 1:
I researched a little bit more and found that Membership.GetUser invokes the default constructor of the MongoDbMembershipProvider. So, I changed the default constructor to the following:
public MongoDbMembershipProvider()
{
_userRepository = new UserRepository();
_roleService = new RoleService();
_authenticationService = new AuthenticationService(_userRepository);
}
It seems to be passing the test right now! Although I am interested if there is a better solution.