mercredi 24 janvier 2018

Integration tests with identity framework

I am trying to set up integration tests with identity framework. I have a DbContext that inherits IdentityDbContext like this:

public class DatabaseContext : IdentityDbContext<User>, IClientConfigurationDbContext, IOperationalDbContext, IScopeConfigurationDbContext

And I have created a context in my IntegrationTests application that looks like this:

public class UserContext : LogContext
{
    public IUserProvider UserProvider { get; }
    public UserContext()
    {
        var encryptionProvider = new AdvancedEncryptionStandardProvider(Config);
        var passwordHasher = new PasswordHasher(encryptionProvider);
        var userStore = new UserStore<User>();
        UserProvider = new UserProvider(HttpContext, null, encryptionProvider, passwordHasher, userStore, null, null, null, null, null, null, null);
    }

    public static UserContext GivenServices() => new UserContext();
}

The UserProvider inherits UserManager like this:

public class UserProvider : UserManager, IUserProvider

And my UserManager class is just a custom version of UserManager<> like this:

public class UserManager : UserManager<User>, IUserManager

And the User looks like this:

public class User : IdentityUser
{
    [Required] [MaxLength(100)] public string FirstName { get; set; }
    [Required] [MaxLength(100)] public string LastName { get; set; }
    [Required] [MaxLength(100)] public string Department { get; set; }
    [Required] public bool HasTroposLogin { get; set; }
    [MaxLength(4)] public string Master { get; set; }
    public bool Disabled { get; set; }
    [MaxLength(255)] public string ProfileImageUri { get; set; }

    public DateTime DateCreated { get; set; }
    public DateTime? DateModified { get; set; }
    public DateTime LastLoginDate { get; set; }
}

But when I create my test:

[TestFixture]
public class UserProviderTests
{
    [Test]
    public async Task CanCreateUserAsync()
    {
        var services = UserContext.GivenServices();
        var request = new UserRequestModel
        {
            UserName = "Test",
            Email = "Test@test.com",
            FirstName = "moo",
            LastName = "moo",
            Department = "IT"
        };
        var user = await services.UserProvider.CreateAsync(request);
        user.Id.Should().NotBe(null);
    }
}

and run it, I get an error:

The entity type User is not part of the model for the current context.

Does anyone have any idea why this might be happening?

Aucun commentaire:

Enregistrer un commentaire