2016-02-01 69 views
0

我需要與NSubstitute模擬,並需要設置LoanCreateHandler類的局部變量command模擬數據與它的參數Z 我有這樣的代碼在下面給出:如何使用TestProject中的NSubstitute將值設置爲類的局部變量?

public class ClassA { 
    public string Prop1 { get; set; } 
    public string Prop2 { get; set; } 
    … // Here I have some other properties 
} 

public class CreateLoanCommand { 
    public string X { get; set; } 
    public string Y { get; set; } 
    public ClassA Z { get; set; } 
}  

public class LoanCreateHandler {  

    public Response Handle(LoanCreateRequest request) 
    { 
     var response = CreateTypedResponse(); 
     var command = new CreateLoanCommand 
     { 
      X = request.X, 
      Y = request.Y 
     }; 

     _cqsCommandProcessor.Execute(command); //here I am setting value of command.Z param 

     if (command.Z == null) 
     { 
      //do something 
     }else{ 
      //do another 
     } 

     return true; // returns response 
    } 
} 

所以這裏的時候,我想嘲笑LoanCreateHandler代碼覆蓋。其他循環代碼沒有被覆蓋。請看以下的單元測試:

[TestClass] 
public class LoanCreateHandlerTests 
{ 
    [TestMethod, TestCategory(Tc.Unit)] 
    public void LoanCreateHandler_SuccessTest() 
    { 
     var loanCreateRequest = new LoanCreateRequest 
     { 
      X = "val1", 
      Y = "val2"          
     }; 
     var loanCreateResponse = true; 


     var createLoanCommand = new CreateLoanCommand() 
     { 
      X = "val1", 
      Y = "val2", 
      Z = new ClassA() 
      { 
       Prop1 = "val1", Prop2 = "val2"… 
      } 
     }; 

     _TestHelper.CqsCommandProcessor.Execute(Arg.Any<CreateLoanCommand>()); 

     var loanCreateHandler = new LoanCreateHandler(); 
     loanCreateHandler.Handle(loanCreateRequest).Returns(loanCreateResponse); 
     //here when call goes to Handle() method it creates new LoanCreateRequest object and I want to replace that object with my LoanCreateRequest object, which is created above. 

     Assert.IsNotNull(loanCreateResponse);    
    } 
} 
+0

能否請你澄清你的嘲諷哪一類?請注意,不能將'Returns'用於未使用'Substitute.For'創建的對象,也不能使用非虛擬成員(接口成員正常工作,成員標記爲'virtual'的類也如此)。另外,問題中的測試不檢查'LoanCreateHandler' - 它檢查在測試期間創建的布爾變量('loanCreateResponse')是否爲空。 –

回答

1

您應該使用When..Do回調設置void方法。

我假設_TestHelper.CqsCommandProcessor返回與LoanCreateHandler._cqsCommandProcessor相同的對象。

這就是說,你應該做這樣的事情:

[TestClass] 
public class LoanCreateHandlerTests 
{ 
    [TestMethod, TestCategory(Tc.Unit)] 
    public void LoanCreateHandler_SuccessTest() 
    { 
     var z = new ClassA() 
     { 
      Prop1 = "val1", 
      Prop2 = "val2" 
     }; 

     _TestHelper.CqsCommandProcessor 
      .When(x => x.Execute(Arg.Any<LoanCreateRequest>())) 
      .Do(x => x.Arg<LoanCreateRequest>().Z = z); 

     var loanCreateHandler = new LoanCreateHandler(); 
     var loanCreateRequest = new LoanCreateRequest 
     { 
      X = "val1", 
      Y = "val2" 
     }; 
     var loanCreateResponse = loanCreateHandler.Handle(loanCreateRequest); 

     Assert.IsNotNull(loanCreateResponse); 
    } 
} 
+0

我認爲這是一個很好的答案。也許值得添加一些假設:'_TestHelper.CqsCommandProcessor'必須用'Substitute.For (或類似的,假設類型是一個接口或一個類,其中'Execute'是虛擬的)創建的? –

+0

@Marcio謝謝哥們。當()...做()...爲我工作。我得到了在LoanCreateHandler下設置局部變量值的邏輯。 –

相關問題