question

ItFT-6717 avatar image
0 Votes"
ItFT-6717 asked ItFT-6717 answered

unit test with UserManager


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!

dotnet-csharpdotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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..

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ItFT-6717 avatar image
0 Votes"
ItFT-6717 answered

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ItFT-6717 avatar image
0 Votes"
ItFT-6717 answered

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);
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ItFT-6717 avatar image
0 Votes"
ItFT-6717 answered

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);
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.