我正在使用.Net Core构建一些Web API。在测试我的一种服务方法时,我遇到了一个问题,即在模拟的DbSet上调用Remove()似乎并没有删除它应该删除的对象。返回简单的Contains()布尔之后,检查我的设置。
尝试以不同的方式模拟它,但似乎没有一个起作用
我的DbSet模拟助手:
public static Mock<DbSet<T>> CreateDbSetMock<T>(IEnumerable<T> elements) where T : class
{
var elementsAsQueryable = elements.AsQueryable();
var dbSetMock = new Mock<DbSet<T>>();
dbSetMock.As<IQueryable<T>>().Setup(m => m.Provider).Returns(elementsAsQueryable.Provider);
dbSetMock.As<IQueryable<T>>().Setup(m => m.Expression).Returns(elementsAsQueryable.Expression);
dbSetMock.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(elementsAsQueryable.ElementType);
dbSetMock.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(elementsAsQueryable.GetEnumerator());
return dbSetMock;
}
}
我要测试的方法:
public virtual async Task<bool> DeleteSupplierAsync(User user)
{
var currentRoles = await _userManagerWrap.GetRolesForUser(_userManager, user);
if (!currentRoles.Contains(nameof(Role.Supplier)))
{
throw new ArgumentException("Provided user does not belong to Suppliers");
}
var brand = GetBrandForUser(user);
if (brand != default(Brand))
{
var users = _context.Users.Where(e => e.BrandId == brand.Id).ToList();
_context.Users.RemoveRange(users);
_context.Brands.Remove(brand);
}
else
{
_context.Users.Remove(user);
}
await _context.SaveChangesAsync();
//In the next line Users still contains the user i want to remove
return !_context.Users.Contains(user);
}
我的测试:
public async Task DeleteSupplierAsyncTest(IList<string> rolesToReturn, Brand brandToReturn, List<User> usersToReturn, List<Brand> brandsToReturn, string userIdToDelete, bool expectedException, string expectedMessage, bool expectedResult)
{
var userManagerMock = new Mock<UserManagerWrap>();
userManagerMock.Setup(m => m.GetRolesForUser(It.IsAny<UserManager<User>>(), It.IsAny<User>())).Returns(Task.FromResult(rolesToReturn));
var appDbContextMock = new Mock<AppDbContext>();
var userTableMock = TestsHelper.CreateDbSetMock(usersToReturn);
var brandsTableMock = TestsHelper.CreateDbSetMock(brandsToReturn);
appDbContextMock.Setup(m => m.Users).Returns(userTableMock.Object);
appDbContextMock.Setup(m => m.Brands).Returns(brandsTableMock.Object);
var userServiceMock = new Mock<UserService>(appDbContextMock.Object, null, null, new Mock<IOptions<TokenSettings>>().Object, null, null, new Mock<IOptions<AzureAdClaimTypes>>().Object, userManagerMock.Object, null, null) { CallBase = true };
userServiceMock.Setup(m => m.GetBrandForUser(It.IsAny<User>())).Returns(brandToReturn);
if (expectedException)
{
var ex = Assert.ThrowsAsync(Is.AssignableTo(typeof(Exception)), () => userServiceMock.Object.
DeleteSupplierAsync(null));
Assert.AreEqual(expectedMessage, ex.Message);
}
else
{
var user = userTableMock.Object.First(x => x.Id == userIdToDelete);
var result = await userServiceMock.Object.DeleteSupplierAsync(user);
Assert.AreEqual(expectedResult, result);
}
}
当对象成功从集合中移除时,我期望输出为true,因此当方法返回true时。