[英]Unit testing automapper to return count of users in a role
I would like to unit test
this Controller
.我想对这个Controller
进行unit test
。 It returns the name of the role
and the count
of users
that belong to that role
:它返回role
的名称和属于该role
的users
count
:
[HttpGet]
[EnableQuery]
public IEnumerable<RoleDto> Get()
{
var roles = dbContext.Roles.ToList();
return roles.Select(r =>
{
var dto = mapper.Map<RoleDto>(r);
dto.UserCount = dbContext.UserRoles.Count(x => x.RoleId == r.Id);
return dto;
}).ToList();
}
And here is my test - The DTO
is always null
and later throws an exception
.这是我的测试 - DTO
始终为null
,稍后会引发exception
。 I am unable to map
this properly.我无法正确map
它。
It is a simple unit test
that creates a user
, creates roles
and add the user
to those roles
.这是一个简单的unit test
,用于创建user
、创建roles
并将user
添加到这些roles
。
It then checks if a user
has been added to that role
.然后检查user
是否已添加到该role
。
[Test]
public async Task Get_Should_Return_List_OfRoles()
{
TestSetup();
var mapperMock = new Mock<IMapper>();
IdentityRole role = null;
mapperMock.Setup(u => u.Map<RoleDto>(It.IsAny<IdentityRole>()))
.Callback<object>(inputargs => role = inputargs as IdentityRole);
var roleManagerWrapperMock = new Mock<IRoleManagerWrapper>();
var admin = new IntentUser
{
UserName = "Administrator2",
Email = "admin2@intent2.app",
EmailConfirmed = true,
IsEnabled = true,
IsSystemUser = true
};
var adminRole = new IdentityRole()
{
Name = "Admin"
};
var managerRole = new IdentityRole()
{
Name = "Manager"
};
ApplicationDbContext.Users.Add(admin);
ApplicationDbContext.Roles.Add(managerRole);
ApplicationDbContext.Roles.Add(adminRole);
ApplicationDbContext.SaveChanges();
var adminRoleAdded = new IdentityUserRole<string>()
{
RoleId = adminRole.Id,
UserId = admin.Id
};
var managerRoleAdded = new IdentityUserRole<string>()
{
RoleId = managerRole.Id,
UserId = admin.Id
};
ApplicationDbContext.UserRoles.Add(adminRoleAdded);
ApplicationDbContext.UserRoles.Add(managerRoleAdded);
ApplicationDbContext.SaveChanges();
var sut = new RolesController(roleManagerWrapperMock.Object, ApplicationDbContext, mapperMock.Object);
var result = sut.Get();
foreach (var roleDto in result)
{
Assert.AreEqual(roleDto.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);
}
As mentioned in the comments, you can use an actual mapper instead of trying to mock it.正如评论中提到的,您可以使用实际的映射器而不是尝试模拟它。
//...
//Configure mapping just for this test but something like this
//should be in accessible from your composition root and called here.
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<RoleDto, IdentityRole>();
cfg.CreateMap<IdentityRole, RoleDto>();
});
IMapper mapper = config.CreateMapper();
// or
//IMapper mapper = new Mapper(config);
//...
Reference Automapper: Getting Started Guide参考Automapper:入门指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.