【问题标题】:Test a business logic which calls a public method that internally calls a private method测试调用内部调用私有方法的公共方法的业务逻辑
【发布时间】:2017-03-25 09:35:39
【问题描述】:

我在这样的接口中有一个方法:

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 表存储进行检查。

我正在尝试对 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!!!
    }
}

非常感谢任何帮助,并将帮助我开始这条道路。 提前致谢。

问候。

【问题讨论】:

    标签: unit-testing asynchronous moq


    【解决方案1】:

    您必须模拟对存储的访问,而不是模拟您的业务逻辑。为此,您必须再创建一层:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多