简体   繁体   English

如何设置模拟 <UserManager<TUser > &gt;

[英]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.

相关问题 如何模拟UserManager <IdentityUser> - How to mock UserManager<IdentityUser> 如何模拟SignInManager <TUser, TKey> .HasBeenVerifiedAsync()方法 - How to mock SignInManager<TUser, TKey>.HasBeenVerifiedAsync() method 如何对需要 UserManager 的 ASP .NET Core 3.0+ Razor 页面处理程序进行单元测试<tuser>和登录管理器<tuser> ?</tuser></tuser> - How do I unit test ASP .NET Core 3.0+ Razor Page handlers that require UserManager<TUser> and SignInManager<TUser>? 单元测试:用户管理器<applicationuser>模拟设置 FindByIdAsync 返回 NULL</applicationuser> - UnitTests: UserManager<ApplicationUser> Mock Setup FindByIdAsync Returns NULL 如何在 .Net Core 测试中模拟 UserManager? - How to mock UserManager in .Net Core testing? 我如何模拟UserManager.GetRoles() - How do I mock UserManager.GetRoles() 如何使用NUnit在.NET Core中模拟UserManager? - How to mock UserManager in .NET Core with NUnit? 如何模拟ASP.NET 5中的UserManager - How to mock out the UserManager in ASP.NET 5 对依赖于UserManager的控制器进行单元测试的最佳实践 <TUser> ? - Best Practice for Unit Testing a Controller that depends on UserManager<TUser>? UserManager 中没有可见的实现细节<tuser> ASP.net 内核中的 class</tuser> - No implementation details visible in UserManager<TUser> class in ASP.net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM