Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing type map configuration or unsupported mapping on IdentityRole

I am writing unit tests and this mapper is causing me grief again. I understood from a previous post that I cannot Mock the mapper, i have to use it straight away. So I have created maps but it says missing type map configuration.

    public RoleDto GetSingle([FromRoute] string id)
    {
        var r = roleManagerWrapper.GetSingleRole(id);
        return mapper.Map<RoleDto>(r);
    }

It breaks when it tries to map the object. Is there any special mapping for Task <IdentityRole> that needs to be implemented?

    public async Task<IdentityRole> GetSingleRole(string roleId)
    {
        var role = await this.roleManager.Roles.SingleOrDefaultAsync(r => r.Id == roleId);

        return role;
    }

Here is my test that only counts the number of roles that are created.

    [Test]
    public async Task Get_Single()
    {
        TestSetup();

        var roleManagerWrapperMock = new Mock<IRoleManagerWrapper>();


        var adminRole = new IdentityRole()
        {
            Name = "Admin",
            Id = "4a8de423-5663-4831-ac07-7ce92465b008"
        };

        var managerRole = new IdentityRole()
        {
            Name = "Manager",
            Id = "40f74162-3359-4253-9b5a-ad795b328267"
        };
        ApplicationDbContext.Roles.Add(managerRole);
        ApplicationDbContext.Roles.Add(adminRole);

        ApplicationDbContext.SaveChanges();

        var sut = new RolesController(roleManagerWrapperMock.Object, ApplicationDbContext, Mapper);
        var result = sut.GetSingle("4a8de423-5663-4831-ac07-7ce92465b008");
        Assert.AreEqual(result.UserCount, 1);
    }


       protected void TestSetup(string databaseName = null)
       {
        if (databaseName == null)
        {
            databaseName = GetTestName();
        }

        TestCleanup();
        ServiceProvider = new ServiceCollection()
            .AddEntityFrameworkInMemoryDatabase()
            .BuildServiceProvider();

        dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
            .UseInMemoryDatabase(databaseName)
            .UseInternalServiceProvider(ServiceProvider)
            .Options;

        ApplicationDbContext = new ApplicationDbContext(dbContextOptions);

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<RoleDto, IdentityRole>();
            cfg.CreateMap<IdentityRole, RoleDto>();
            cfg.CreateMap<CreateRoleDto, IdentityRole>().ReverseMap();
            cfg.CreateMap<UpdateRoleDto, IdentityRole>().ReverseMap();
        });
         Mapper = config.CreateMapper();
    }
like image 259
grozdeto Avatar asked Nov 21 '25 21:11

grozdeto


1 Answers

The action needs to be refactored to use proper asyn syntax since GetSingleRole returns Task<IdentityRole>

public Task<RoleDto> GetSingle([FromRoute] string id) {
    IdentityRole r = await roleManagerWrapper.GetSingleRole(id);
    return mapper.Map<RoleDto>(r);
}

And the test updated accordingly

[Test]
public async Task Get_Single() {
    //Arrange
    TestSetup();

    var roleManagerWrapperMock = new Mock<IRoleManagerWrapper>();

    //...omitted for brevity

    var sut = new RolesController(roleManagerWrapperMock.Object, ApplicationDbContext, Mapper);

    //Act
    RoleDto result = await sut.GetSingle("4a8de423-5663-4831-ac07-7ce92465b008");

    //Assert
    Assert.AreEqual(result.UserCount, 1);
}
like image 60
Nkosi Avatar answered Nov 23 '25 10:11

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!