11

I am writing a UI for managing users in an ASP.NET 5 app. I need to show any errors returned by the UserManager in the UI. I have the IdentityResult errors being passed back in the view model but I am a touch adrift when it comes to testing my code.

What is the best way to Mock the UserManager in ASP.NET 5?

Should I be inheriting from UserManager and overriding all the methods I am using and then injecting my version of UserManager into an instance of the Controller in my test project?

2
  • 1
    Have you found a solution for this? I am trying to create a Unit Test for my Account Controller. Commented Jan 9, 2016 at 18:10
  • Basically just decided to wait for xunit support. Commented Jan 9, 2016 at 22:30

1 Answer 1

20

I have managed it with the help of the MVC Music Store sample application.

In my Unit Test class, I set up the database context and UserManager like this:

public class DatabaseSetupTests : IDisposable { private MyDbContext Context { get; } private UserManager<ApplicationUser> UserManager { get; } public DatabaseSetupTests() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<MyDbContext>(options => options.UseInMemoryDatabase()); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<MyDbContext>(); // Taken from https://github.com/aspnet/MusicStore/blob/dev/test/MusicStore.Test/ManageControllerTest.cs (and modified) // IHttpContextAccessor is required for SignInManager, and UserManager var context = new DefaultHttpContext(); context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature()); services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context }); var serviceProvider = services.BuildServiceProvider(); Context = serviceProvider.GetRequiredService<MyDbContext>(); UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>(); } .... } 

Then I can use the UserManager in my unit tests, for example:

[Fact] public async Task DontCreateAdminUserWhenOtherAdminsPresent() { await UserManager.CreateAsync(new ApplicationUser { UserName = "[email protected]" }, "IDoComplyWithTheRules2016!"); ... } 

If your Dependency Injector is not able to resolve an IHttpContextAccessor then you will not be able to create a UserManager instance due to it being dependent on it. I think (and this is just an assumption), that with Asp.Net 5, the UserManager does take care of refreshing cookie based claims when you change them (claims, roles...) for a user and therefore requires some HttpContext for login / logout actions and cookie access.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for providing the link to the source - in my application, your code made UserManager.GetUserAsync return null, but after reconstructing it from the MusicStore example i got it working. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.