2016-11-11 6 views
1

プライベートメソッドを呼び出し、パブリックメソッドを呼び出すビジネスロジック:テスト内部で、私はそうのようなインタフェースのメソッドを持って

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 Storageを照会してチェックします。

私はユニットテスト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!!! 
    } 
} 

ご協力いただきまして誠にありがとうございます。 ありがとうございます。

よろしくお願いいたします。

答えて

2

ビジネスロジックではなく、ストレージへのアクセスをモックする必要があります。

[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 
} 

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; 
    } 
} 

は、その後、あなたのストレージへのアクセスを模擬することができます。これを実現するには、もう一つの層を作成する必要があります

関連する問題