[英]How to Setup Mock<UserManager<TUser >>
How to setup Mock<UserManager<ApplicationUser>> _userManager
so _userManager.FindByIdAsync(userId)
goes to ApplicationDbContext and finds the user by Id like this _context.Users.SingleOrDefault(u=>u.Id == userId)
? 如何设置Mock<UserManager<ApplicationUser>> _userManager
以便_userManager.FindByIdAsync(userId)
转到ApplicationDbContext并按_context.Users.SingleOrDefault(u=>u.Id == userId)
这样的ID查找用户?
My code: 我的代码:
[TestClass]
public class AccountControllerTest
{
private ApplicationDbContext _context;
private Mock<UserManager<ApplicationUser>> _userManager;
private IHostingEnvironment _enviroment;
private Referrals _referrals;
private Mock<IEmailSender> _emailSender;
private Mock<IUserNameGenerator> _userNameGenerator;
private Mock<IUrlHelper> _urlHelper;
private Mock<SignInManager<ApplicationUser>> _signInManager;
private TimeSpan _startTrialTime;
[TestInitialize]
public void Init()
{
_userManager = UserManagerAndDbMocker.GetMockUserManager();
_context = UserManagerAndDbMocker.ContextInMemoryMocker();
_enviroment = new HostingEnvironment() { EnvironmentName = "Development" };
_referrals = new Referrals(_context, _userManager.Object);
_emailSender = new Mock<IEmailSender>();
_userNameGenerator = new Mock<IUserNameGenerator>();
_urlHelper = new Mock<IUrlHelper>();
_signInManager = new Mock<SignInManager<ApplicationUser>>();
UserManagerSetup();
}
private void UserManagerSetup()
{
_userManager.Setup(um => um.CreateAsync(
It.IsAny<ApplicationUser>(),
It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Success));
_userManager.Setup(um => um.ConfirmEmailAsync(
It.IsAny<ApplicationUser>(),
It.IsAny<string>()))
.Returns(
Task.FromResult(IdentityResult.Success));
_userManager.Setup(um => um.FindByIdAsync(It.IsAny<string>()));
}
I am stuck on mocking FindByIdAsync
. 我一直在嘲笑FindByIdAsync
。 I want when I test _userManager.FindById(userId)
to return _context.Users.SingleOrDefault(u=>u.Id == userId)
. 我想在测试_userManager.FindById(userId)
时返回_context.Users.SingleOrDefault(u=>u.Id == userId)
。
public static class UserManagerAndDbMocker
{
public static Mock<UserManager<ApplicationUser>> GetMockUserManager()
{
var userStoreMock = new Mock<IUserStore<ApplicationUser>>();
return new Mock<UserManager<ApplicationUser>>(
userStoreMock.Object, null, null, null, null, null, null, null, null);
}
public static ApplicationDbContext ContextInMemoryMocker()
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseInMemoryDatabase();
var context = new ApplicationDbContext(optionsBuilder.Options);
return context;
}
}
How can I achieve that? 我该如何实现?
If I understand your question correctly, this should work for you: 如果我正确理解了您的问题,这应该对您有用:
_userManager
.Setup(um => um.FindByIdAsync(It.IsAny<string>()))
.Returns( (string userId) => _context.Users.SingleOrDefault(u => u.Id == userId));
In Returns method you can specify lambda that uses your actual input parameter. 在返回方法中,您可以指定使用实际输入参数的lambda。
See also MOQ: Returning value that was passed into a method 另请参见MOQ:返回传递给方法的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.