简体   繁体   中英

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) ?

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 . I want when I test _userManager.FindById(userId) to return _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.

See also MOQ: Returning value that was passed into a method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM