0

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?

arghtype
  • 4,376
  • 11
  • 45
  • 60
Jones
  • 375
  • 1
  • 4
  • 14
  • The question in its current state is unclear as it is incomplete. Read [ask] and then provide a [mcve] that can be used to better understand your problem. – Nkosi Oct 17 '17 at 14:10
  • Hi @Jones, give us and example of your test code or paste it completely and we would be able to help you go in the right direction. Answer to this comments and I'll take a look ( by the way, are you familiar with Moq library?) – Juan Oct 17 '17 at 14:22

1 Answers1

1

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

arghtype
  • 4,376
  • 11
  • 45
  • 60