如何在 AddClaimsAsync 方法中模拟 IEnumerable 以便 运行 单元测试
How to mock IEnumerable in AddClaimsAsync method in order to run a unit test
我需要模拟 AddClaimsAsync()
方法,这里就是这个方法。我对 IEnumerable
有疑问。阅读后我发现如果我模拟一个对象然后将它添加到一个集合中它应该可以工作。:
public async Task<IdentityResult> AddClaimsAsync(IntentUser user, IEnumerable<Claim> claims)
{
return await this.userManager.AddClaimsAsync(user, claims);
}
这是测试:
var itemMock = new Mock<Claim>();
var items = new List<Claim> {itemMock.Object};
userManagerWrapperMock.Setup(u => u.AddClaimsAsync(admin, items)).ReturnsAsync(admin, new[]
{
new Claim(JwtClaimTypes.Name, dto.FirstName),
new Claim(JwtClaimTypes.GivenName, dto.FirstName),
new Claim(JwtClaimTypes.FamilyName, dto.FamilyName),
new Claim(JwtClaimTypes.Email, dto.Email),
new Claim(JwtClaimTypes.EmailVerified, dto.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
});
这是Controller中需要mock的方法:
await userManagerWrapper.AddClaimsAsync(usr, new[]
{
new Claim(JwtClaimTypes.Name, userDto.FirstName),
new Claim(JwtClaimTypes.GivenName, userDto.FirstName),
new Claim(JwtClaimTypes.FamilyName, userDto.FamilyName),
new Claim(JwtClaimTypes.Email, user.Email),
new Claim(JwtClaimTypes.EmailVerified, user.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
});
我得到的错误是:
'ISetup<IUserManagerWrapper, Task<IdentityResult>>
' does not contain a
definition for 'ReturnsAsync
' and the best extension method overload
'ReturnsExtensions.ReturnsAsync<IUserManagerWrapper,
IntentUser>(IReturns<IUserManagerWrapper, ValueTask<IntentUser>>,
IntentUser, TimeSpan)
' requires a receiver of type
'IReturns<IUserManagerWrapper, ValueTask<IntentUser>>
'
我不需要 return 任何东西,这是一个无效的方法。
我也不需要模拟任何东西。这很有效。
userManagerWrapperMock.Setup(u => u.AddClaimsAsync(admin, new[]
{
new Claim(JwtClaimTypes.Name, dto.FirstName),
new Claim(JwtClaimTypes.GivenName, dto.FirstName),
new Claim(JwtClaimTypes.FamilyName, dto.FamilyName),
new Claim(JwtClaimTypes.Email, dto.Email),
new Claim(JwtClaimTypes.EmailVerified, dto.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
}));
我需要模拟 AddClaimsAsync()
方法,这里就是这个方法。我对 IEnumerable
有疑问。阅读后我发现如果我模拟一个对象然后将它添加到一个集合中它应该可以工作。:
public async Task<IdentityResult> AddClaimsAsync(IntentUser user, IEnumerable<Claim> claims)
{
return await this.userManager.AddClaimsAsync(user, claims);
}
这是测试:
var itemMock = new Mock<Claim>();
var items = new List<Claim> {itemMock.Object};
userManagerWrapperMock.Setup(u => u.AddClaimsAsync(admin, items)).ReturnsAsync(admin, new[]
{
new Claim(JwtClaimTypes.Name, dto.FirstName),
new Claim(JwtClaimTypes.GivenName, dto.FirstName),
new Claim(JwtClaimTypes.FamilyName, dto.FamilyName),
new Claim(JwtClaimTypes.Email, dto.Email),
new Claim(JwtClaimTypes.EmailVerified, dto.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
});
这是Controller中需要mock的方法:
await userManagerWrapper.AddClaimsAsync(usr, new[]
{
new Claim(JwtClaimTypes.Name, userDto.FirstName),
new Claim(JwtClaimTypes.GivenName, userDto.FirstName),
new Claim(JwtClaimTypes.FamilyName, userDto.FamilyName),
new Claim(JwtClaimTypes.Email, user.Email),
new Claim(JwtClaimTypes.EmailVerified, user.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
});
我得到的错误是:
'
ISetup<IUserManagerWrapper, Task<IdentityResult>>
' does not contain a definition for 'ReturnsAsync
' and the best extension method overload 'ReturnsExtensions.ReturnsAsync<IUserManagerWrapper, IntentUser>(IReturns<IUserManagerWrapper, ValueTask<IntentUser>>, IntentUser, TimeSpan)
' requires a receiver of type 'IReturns<IUserManagerWrapper, ValueTask<IntentUser>>
'
我不需要 return 任何东西,这是一个无效的方法。
我也不需要模拟任何东西。这很有效。
userManagerWrapperMock.Setup(u => u.AddClaimsAsync(admin, new[]
{
new Claim(JwtClaimTypes.Name, dto.FirstName),
new Claim(JwtClaimTypes.GivenName, dto.FirstName),
new Claim(JwtClaimTypes.FamilyName, dto.FamilyName),
new Claim(JwtClaimTypes.Email, dto.Email),
new Claim(JwtClaimTypes.EmailVerified, dto.EmailConfirmed.ToString(), ClaimValueTypes.Boolean)
}));