jeudi 25 février 2016

ASP.NET 5 Unit Test controller with UserManager

I started to use ASP.NET 5 (vNext) and now I'm trying to unit test a controller that uses UserManager to register and login users.

I'm using xUnit and moq.netcore.

In my UnitTest I have:

var mockStore = new Mock<IUserStore<ApplicationUser>>(MockBehavior.Strict).As<IUserPasswordStore<ApplicationUser>>();

mockStore.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>(), CancellationToken.None)).Returns(Task.FromResult(new IdentityResult()));

var userManager = new UserManager<ApplicationUser>(mockStore.Object, null, null, null, null, null, null, null, null, null);

And the code in the controller:

var result = await _securityManager.CreateAsync(user, model.Password);

But, when I run the unit test I get a null exception, when calling CreateAsync.

I don't understand why I can't Mock the CreateAsync method with ApplicationUser and string parameters, and I have to mock it with the CancellationToken.

It's my first project using ASP.NET 5, I was using ASP.NET 4.6 before, so a lot of things are different now.

And, do you think I should already develop an important project in ASP.NET 5 or should I wait and develop it for ASP.NET 4.6?

Aucun commentaire:

Enregistrer un commentaire