我有以下界面及其存储库类
界面
public interface IIdentityRepository
{
bool CreateUser(ApplicationUser user, string password);
}
存储库
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()))
{
}
}
这是我尝试为CreateUser
方法编写的测试类
在这种方法中,我使用AppplicationUser
作为我的模型。
[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
}
但是在这里我遇到了编译时错误
编辑:
一旦我更改了上面的错误行,如下错误就消失了。
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password")).Callback<ApplicationUser>(c => _users.Add(c));
但是当我运行此测试时,我收到了以下错误
结果消息:初始化方法 ProjectName.UnitTest.Common.IdentityRepositoryTest.TestSetup扔了 例外。 System.ArgumentException:System.ArgumentException:无效 打回来。使用参数设置方法(ApplicationUser,String) 无法使用参数调用回调(ApplicationUser)..
答案 0 :(得分:0)
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
.Callback<ApplicationUser>(c => _users.Add(c));
应该是:
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
.Callback<ApplicationUser, string>((c, s) => _users.Add(c));