模拟存储库返回 null
Mock repository is returning null
我正在为服务编写一个简单的 xunit 测试用例 class。
我嘲笑了 _userAPIRepository
但它 returns null 值。它应该 return teacherDetails
.
下面是服务class方法和xunit测试用例方法。
服务方式
public async Task<List<TeacherDto>> GetTeacherDetails(Teacher teacher)
{
List<TeacherDto> teacherDetails = new List<TeacherDto>();
UserByUsername userByUsername = new UserByUsername
{
Username = teacher.Name,
Id = teacher.Id
};
teacherDetails = await _userServiceAPI.GetUserIdsByName(userByUsername);
return teacherDetails;
}
xunit 测试用例
[Fact()]
public async Task GetUserDetails()
{
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
UserByUsername userByUsername = new UserByUsername
{
Username = teacher.Name,
Id = teacher.Id
};
_userAPIRepository.Setup(x => x.GetUserIdsByName(userByUsername)).Returns(Task.FromResult(teacherDetails);
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
当参数设置与执行测试时传入的参数不匹配时,模拟 returns 默认为 null。在呈现的案例中,使用了一个实际的 instance/reference,它与在测试对象中创建的实例不匹配。
使用参数匹配器从模拟中获取所需的行为,因为参数是在被测对象中初始化的。
[Fact()]
public async Task GetUserDetails() {
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
_userAPIRepository
.Setup(x => x.GetUserIdsByName(It.Is<UserByUsername>(u =>
u.Username == teacher.Name && u.Id == teacher.Id)))
.ReturnsAsync(teacherDetails);
//Act
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
注意使用 It.Is<>
添加具有特定 filter/predicate 的参数匹配器。
如果不需要实际匹配成员而只接受任何UserByUsername
,则可以使用It.IsAny<>
代替
例如
[Fact()]
public async Task GetUserDetails() {
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
_userAPIRepository
.Setup(x => x.GetUserIdsByName(It.IsAny<UserByUsername>()))
.ReturnsAsync(teacherDetails);
//Act
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
参考 Moq Quickstart 以更好地了解如何使用该库
我正在为服务编写一个简单的 xunit 测试用例 class。
我嘲笑了 _userAPIRepository
但它 returns null 值。它应该 return teacherDetails
.
下面是服务class方法和xunit测试用例方法。
服务方式
public async Task<List<TeacherDto>> GetTeacherDetails(Teacher teacher)
{
List<TeacherDto> teacherDetails = new List<TeacherDto>();
UserByUsername userByUsername = new UserByUsername
{
Username = teacher.Name,
Id = teacher.Id
};
teacherDetails = await _userServiceAPI.GetUserIdsByName(userByUsername);
return teacherDetails;
}
xunit 测试用例
[Fact()]
public async Task GetUserDetails()
{
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
UserByUsername userByUsername = new UserByUsername
{
Username = teacher.Name,
Id = teacher.Id
};
_userAPIRepository.Setup(x => x.GetUserIdsByName(userByUsername)).Returns(Task.FromResult(teacherDetails);
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
当参数设置与执行测试时传入的参数不匹配时,模拟 returns 默认为 null。在呈现的案例中,使用了一个实际的 instance/reference,它与在测试对象中创建的实例不匹配。
使用参数匹配器从模拟中获取所需的行为,因为参数是在被测对象中初始化的。
[Fact()]
public async Task GetUserDetails() {
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
_userAPIRepository
.Setup(x => x.GetUserIdsByName(It.Is<UserByUsername>(u =>
u.Username == teacher.Name && u.Id == teacher.Id)))
.ReturnsAsync(teacherDetails);
//Act
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
注意使用 It.Is<>
添加具有特定 filter/predicate 的参数匹配器。
如果不需要实际匹配成员而只接受任何UserByUsername
,则可以使用It.IsAny<>
代替
例如
[Fact()]
public async Task GetUserDetails() {
//Arrange
List<TeacherDto> teacherDetails = ServiceDataSource.teacherDetails();
Teacher teacher = ServiceDataSource.teacher();
_userAPIRepository
.Setup(x => x.GetUserIdsByName(It.IsAny<UserByUsername>()))
.ReturnsAsync(teacherDetails);
//Act
var result = await _userServiceAPI.GetTeacherDetails(teacher);
//Assert
Assert.True(result.Count > 0);
}
参考 Moq Quickstart 以更好地了解如何使用该库