edua_glz
edua_glz

Reputation: 1177

Unit Test IdentityDbContext

I´ve read that the new EF7 allows in memory storage that makes easier the unit testing. This works great on a DbContext, but the problem that I have so far is that I'm using a IdentityDbContext, which doesn´t come with that option, or I haven't found it so far.

What I'm trying to do is to add unit test to my AccountController which depends on the new UserManager and SignInManager classes. I would like to test against an In-Memory list but I still don´t know how to make those clases use a dbcontext different than the EF-SQL IdentityDbContext. Is there any way around this?

Upvotes: 3

Views: 2266

Answers (2)

agua from mars
agua from mars

Reputation: 17404

Dec. 27th 2015 - Update for RC1

The project.json

{
  "version": "1.0.0-*",
  "dependencies": {
    "{assembly under test}": "",
    "Microsoft.AspNet.Hosting": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-*"
  },
  "commands": {
    "test": "xunit.runner.dnx"
  },
  "frameworks": {
    "dnx451": {
      "dependencies": {
        "Moq": "4.2.1312.1622"
      }
    }
  }
}

A test class sample to test AccountController:

using {Your NS}.Controllers;
using Microsoft.AspNet.Identity;
using Moq;
using Xunit;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.OptionsModel;
using System.Threading;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.Extensions.Logging;

namespace NS.test.Controllers
{
    public class TestRole
    {
        public string Id { get; private set; }
        public string Name { get; set; }
    }

    public interface ITestUserStore<T>:IUserStore<T>, IUserPasswordStore<T> where T :class
    { }

    public class AccountControllerTest
    {
        private static UserManager<TUser> GetUserManager<TUser>(List<IUserValidator<TUser>> userValidators) where TUser : class
        {
            var store = new Mock<ITestUserStore<TUser>>();
            store.Setup(s => s.CreateAsync(It.IsAny<TUser>(), It.IsAny<CancellationToken>())).ReturnsAsync(IdentityResult.Success);
            var options = new Mock<IOptions<IdentityOptions>>();
            var idOptions = new IdentityOptions();
            idOptions.Lockout.AllowedForNewUsers = false;
            options.Setup(o => o.Value).Returns(idOptions);
            var pwdValidators = new List<PasswordValidator<TUser>>();
            pwdValidators.Add(new PasswordValidator<TUser>());
            var userManager = new UserManager<TUser>(store.Object, options.Object, new PasswordHasher<TUser>(),
                userValidators, pwdValidators, new UpperInvariantLookupNormalizer(),
                new IdentityErrorDescriber(), null,
                new Mock<ILogger<UserManager<TUser>>>().Object,
                null);

            return userManager;
        }

        private static Mock<SignInManager<TUser>> MockSigninManager<TUser>(UserManager<TUser> userManager) where TUser : class
        {            
            var context = new Mock<HttpContext>();
            var contextAccessor = new Mock<IHttpContextAccessor>();
            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);

            var roleManager = new RoleManager<TestRole>(new Mock<IRoleStore<TestRole>>().Object,new RoleValidator<TestRole>[] { new RoleValidator<TestRole>() }, null, null, null, null);
            var identityOptions = new IdentityOptions();
            var options = new Mock<IOptions<IdentityOptions>>();
            options.Setup(a => a.Value).Returns(identityOptions);
            var claimsFactory = new UserClaimsPrincipalFactory<TUser, TestRole>(userManager, roleManager, options.Object);
            return new Mock<SignInManager<TUser>>(userManager, contextAccessor.Object, claimsFactory, options.Object, null);
        }


        [Fact]
        public async Task RegisterTest()
        {
            var userValidators = new List<IUserValidator<YourUser>>();
            var validator = new Mock<IUserValidator<YourUser>>();
            userValidators.Add(validator.Object);
            var userManager = GetUserManager(userValidators);

            validator.Setup(v => v.ValidateAsync(userManager, It.IsAny<ChatLeUser>()))
               .Returns(Task.FromResult(IdentityResult.Success)).Verifiable();

            var signinManager = MockSigninManager<YourUser>(userManager);
            signinManager.Setup(m => m.SignInAsync(It.IsAny<YourUser>(), It.IsAny<bool>(), null)).Returns(Task.FromResult(0)).Verifiable();
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
            using (var controller = new AccountController(userManager, signinManager.Object) { ViewData = viewData })
            {
                var result = await controller.Register(new RegisterViewModel()
                {
                    ConfirmPassword = "test123",
                    Password = "Test123-123",
                    UserName = "test"
                });

                Assert.IsType<RedirectToActionResult>(result);
            }
        }
    }
}

Upvotes: 3

Matt DeKrey
Matt DeKrey

Reputation: 11932

Ultimately, you'll probably want to mock the UserManager and SignInManager directly. Unfortunately, many of those methods under beta-3 aren't virtual; you'll be a bit stuck there. However, you can see from the source of UserManager and SignInManager that the Identity team appears to be addressing those for the next release.

I personally have several tests not compiling because of the same issue.

Upvotes: 1

Related Questions