测试调用内部调用私有方法的 public 方法的业务逻辑
Test a business logic which calls a public method that internally calls a private method
我在接口中有一个方法,如下所示:
Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity);
实施:
public async Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity)
{
// Check if the Student is activated for course
var checkStudentForCourseTask = await this.CheckIfStudentIsEnabledForCourseAsync(studentEntity).ConfigureAwait(false);
return checkStudentForCourseTask;
}
private async Task<bool> CheckIfStudentIsEnabledForCourseAsync(StudentEntity studentEntity)
{
var result = await this.tableStorage.RetrieveAsync<StudentTableEntity>(StudentEntity.Id, StudentEntity.CourseId, this.tableName).ConfigureAwait(false);
return result != null && result.IsActivated;
}
CheckIfStudentIsEnabledForCourseAsync 是通过查询 Azure Table 存储进行检查的私有方法。
我正在尝试对 IsStudentAuthorizedAsync 进行单元测试,但在初始设置调用后无法继续。
[TestClass]
public class AuthorizeStudentServiceBusinessLogicTests
{
private Mock<IAuthorizeStudentServiceBusinessLogic> authorizeStudentServiceBusinessLogic;
[TestMethod]
public async Task IsStudentAuthorizedForServiceAsyncTest()
{
this.authorizeStudentServiceBusinessLogic.Setup(
x => x.IsStudentAuthorizedAsync(It.IsAny<StudentEntity>()))
.Returns(new Task<bool>(() => false));
// What to do next!!!
}
}
非常感谢任何帮助,它们会让我开始这条道路。
提前致谢。
此致。
您必须模拟对存储的访问,而不是您的业务逻辑。为此,您必须再创建一层:
public class Storage : IStorage {
public Task<Student> RetrieveAsync();
}
public class BusinessLogic
{
public BusinessLogic(IStorage storage)
{
_storage = storage;
}
public async Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity)
{
// Check if the Student is activated for course
var checkStudentForCourseTask = await this.CheckIfStudentIsEnabledForCourseAsync(studentEntity).ConfigureAwait(false);
return checkStudentForCourseTask;
}
private async Task<bool> CheckIfStudentIsEnabledForCourseAsync(StudentEntity studentEntity)
{
var result = await _storage.RetrieveAsync<StudentTableEntity>(StudentEntity.Id, StudentEntity.CourseId, this.tableName).ConfigureAwait(false);
return result != null && result.IsActivated;
}
}
然后您就可以模拟对您的存储的访问:
[TestMethod]
public void IsStudentAuthorizedForServiceAsyncTest()
{
Mock<IStorage> storageMock = new Mock<IStorage>();
storageMock.Setup(x => x.Retrieve()).Returns(new Task<Student>()); // Return whatever you need
var target = new BusinessLogic(storageMock.Object);
var actual = target.IsStudentAuthorizedAsync();
// Assert
}
我在接口中有一个方法,如下所示:
Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity);
实施:
public async Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity)
{
// Check if the Student is activated for course
var checkStudentForCourseTask = await this.CheckIfStudentIsEnabledForCourseAsync(studentEntity).ConfigureAwait(false);
return checkStudentForCourseTask;
}
private async Task<bool> CheckIfStudentIsEnabledForCourseAsync(StudentEntity studentEntity)
{
var result = await this.tableStorage.RetrieveAsync<StudentTableEntity>(StudentEntity.Id, StudentEntity.CourseId, this.tableName).ConfigureAwait(false);
return result != null && result.IsActivated;
}
CheckIfStudentIsEnabledForCourseAsync 是通过查询 Azure Table 存储进行检查的私有方法。
我正在尝试对 IsStudentAuthorizedAsync 进行单元测试,但在初始设置调用后无法继续。
[TestClass]
public class AuthorizeStudentServiceBusinessLogicTests
{
private Mock<IAuthorizeStudentServiceBusinessLogic> authorizeStudentServiceBusinessLogic;
[TestMethod]
public async Task IsStudentAuthorizedForServiceAsyncTest()
{
this.authorizeStudentServiceBusinessLogic.Setup(
x => x.IsStudentAuthorizedAsync(It.IsAny<StudentEntity>()))
.Returns(new Task<bool>(() => false));
// What to do next!!!
}
}
非常感谢任何帮助,它们会让我开始这条道路。 提前致谢。
此致。
您必须模拟对存储的访问,而不是您的业务逻辑。为此,您必须再创建一层:
public class Storage : IStorage {
public Task<Student> RetrieveAsync();
}
public class BusinessLogic
{
public BusinessLogic(IStorage storage)
{
_storage = storage;
}
public async Task<bool> IsStudentAuthorizedAsync(StudentEntity studentEntity)
{
// Check if the Student is activated for course
var checkStudentForCourseTask = await this.CheckIfStudentIsEnabledForCourseAsync(studentEntity).ConfigureAwait(false);
return checkStudentForCourseTask;
}
private async Task<bool> CheckIfStudentIsEnabledForCourseAsync(StudentEntity studentEntity)
{
var result = await _storage.RetrieveAsync<StudentTableEntity>(StudentEntity.Id, StudentEntity.CourseId, this.tableName).ConfigureAwait(false);
return result != null && result.IsActivated;
}
}
然后您就可以模拟对您的存储的访问:
[TestMethod]
public void IsStudentAuthorizedForServiceAsyncTest()
{
Mock<IStorage> storageMock = new Mock<IStorage>();
storageMock.Setup(x => x.Retrieve()).Returns(new Task<Student>()); // Return whatever you need
var target = new BusinessLogic(storageMock.Object);
var actual = target.IsStudentAuthorizedAsync();
// Assert
}