unit test with UserManager

ItFT 1 Reputation point
2021-10-23T15:38:02.013+00:00

Hi!
I am new to ASP.NET Core development and need help.
I'm trying to build a unit test on this method:

public static async Task<bool> LoginExiste(UserManager<Utilisateur> userManager, Utilisateur utilisateur)
        {
            var user = await userManager.FindByNameAsync(utilisateur.UserName);
            if(user != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

But I can't seem to build a valid UserManager against my database.
Does anyone have an idea?

Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2021-10-23T15:44:43.907+00:00

    You are writing unit tests incorrectly. The concrete implementation of usermanger should never be called in a unit test.

    You should be passing a mocked usermanger to the method, because it’s mocked, the unit tests knows what it will return..

    0 comments No comments

  2. ItFT 1 Reputation point
    2021-10-23T15:49:05.29+00:00

    This is not my unit test but the method I want to test. Should I modify my method so as not to pass userManager as a parameter?

    0 comments No comments

  3. ItFT 1 Reputation point
    2021-10-23T16:04:52.56+00:00

    These are my tries
    private Mock<UserManager<Utilisateur>> GetMockUserManager()
    {
    var userStoreMock = new Mock<IUserStore<Utilisateur>>();
    return new Mock<UserManager<Utilisateur>>(
    userStoreMock.Object, null, null, null, null, null, null, null, null);
    }

            [Test]
            public async Task LoginExisteTest()
            {
                var utilisateur = new Utilisateur();
                utilisateur.UserName = "fball";
                utilisateur.PasswordHash = "test";
    
                //var options = new DbContextOptionsBuilder<MAuthDbContext>();
                //options.UseSqlServer(@"");
    
    
                //var store = new UserStore<Utilisateur>(new MAuthDbContext(new DbContextOptions<MAuthDbContext>()));
                //var userManager = new UserManager<Utilisateur>(store, null, new PasswordHasher<Utilisateur>(), null, null, null, null, null, null);
                //var userManager = MockHelper.TestUserManager(store);
    
    
    
                Mock<UserManager<Utilisateur>> userManager = GetMockUserManager();
                userManager.Setup(x => x.FindByNameAsync(It.IsAny<string>())).ReturnsAsync(utilisateur);
                bool result = await GestionErreur.LoginExiste(null, utilisateur);
                Assert.IsTrue(result);
            }
    
    0 comments No comments

  4. ItFT 1 Reputation point
    2021-10-23T16:58:04.443+00:00

    ok I found the solution

    public async Task LoginExisteTest()
            {
                var utilisateur = new Utilisateur();
                utilisateur.UserName = "fball";
                utilisateur.PasswordHash = "test";
                Mock<UserManager<Utilisateur>> userManager = GetMockUserManager();
                userManager.Setup(x => x.FindByNameAsync(It.IsAny<string>())).ReturnsAsync(utilisateur);
                bool result = await GestionErreur.LoginExiste(userManager.Object, utilisateur);
                Assert.IsTrue(result);
            }
    
    0 comments No comments