I have following interface and its repository class
Interface
public interface IIdentityRepository
{
bool CreateUser(ApplicationUser user, string password);
}
Repository
public class IdentityRepository : IIdentityRepository
{
ApplicationDbContext dbContext;
public IdentityRepository()
{
dbContext = new ApplicationDbContext(); // if none supplied
}
public bool CreateUser(ApplicationUser user, string password)
{
var userManager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(dbContext));
var idResult = userManager.Create(user, password);
return idResult.Succeeded;
}
}
public class UserManager : UserManager<ApplicationUser>
{
public UserManager()
: base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
}
}
This is the test class I'm trying to write for CreateUser
method
In this method I'm using AppplicationUser
as my model.
[TestClass]
public class IdentityRepositoryTest
{
private IdentityRepository _identitityRepo;
private Mock<IIdentityRepository> _identitityRepository;
private List<ApplicationUser> _users;
// initialize the test class
[TestInitialize]
public void TestSetup()
{
_identitityRepository = new Mock<IIdentityRepository>();
_users = new List<ApplicationUser>();
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>())).Callback<ApplicationUser>(c => _users.CreateUser(c));
_identitityRepo = new IdentityRepository();
}
#region Users
// check valid number of user/s(1) existing in current DB
[TestMethod]
public void IsValidtNumberofUsersExist()
{
// Arrange
_users.Add(new ApplicationUser { UserName = "Kez" , Email = "kez@gmail.com" });
// Act
var result = _identitityRepo.GetAllUsers();
Assert.IsNotNull(result);
// Assert
var numberOfRecords = result.ToList().Count;
Assert.AreEqual(1, numberOfRecords);
}
#endregion
}
But in here I'm having following compile time error
EDIT :
Once I change above error line as follows error went away.
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password")).Callback<ApplicationUser>(c => _users.Add(c));
but when I run this test, I'm getting following error
Result Message: Initialization method ProjectName.UnitTest.Common.IdentityRepositoryTest.TestSetup threw exception. System.ArgumentException: System.ArgumentException: Invalid callback. Setup on method with parameters (ApplicationUser,String) cannot invoke callback with parameters (ApplicationUser)..