2014-03-27 103 views
3

我正在使用Prism的Confirmation類來詢問用戶是否確認。當我單元測試確認的返回值總是錯誤的。也許我可以讓InteractionRequest的setter公開。我的代碼現在看起來像這樣,我的單元測試應該驗證this.copyService.Execute被調用。WPF Prism中的單元測試確認

public InteractionRequest<Confirmation> CopyProjectConfirmationRequest { get; private set; } 

    bool confirmationResult = false; 
    DialogConfirmation dialogConfirmation = new DialogConfirmation 
               { 
               Title = "Copy and Convert Project", 
               Content = string.Format("This Project was created with Version {0} to be used with currentVersion({1}) it must be converted should it copyed and converted Project", projectVersion, toolVersion), 
               ConfirmButtonText = "Copy & Convert", 
               DeclineButtonText = "Cancel" 
               }; 

    this.CopyProjectConfirmationRequest .Raise(dialogConfirmation, cb => { confirmationResult = cb.Confirmed; }); 

    if (confirmationResult) 
    { 
     this.copyService.Execute(this.Model); 
    } 

回答

6

該解決方案已退出簡單。只需在單元測試中加入此項

sut.CopyProjectConfirmationRequest.Raised += (s, e) => 
    { 
    Confirmation context = e.Context as Confirmation; 
    context.Confirmed = true; 
    e.Callback(); 
    }; 
1

你可以嘗試用Moq

Mock<ICopyService> mockService;

嘲諷copyService和測試針對模擬 可能實現....在VM或具有CopyProjectConfirmationRequest屬性的類注入。

mockService.Verify(x => x.Execute(It.Is<Model>(...));

至於CopyProjectConfirmationRequest我會做財產公開,並在單元測試存根它。

+0

我已經這樣做了,因爲在我的測試中confirmationResult始終爲false,所以失敗。 – Kingpin

+0

保存'CopyProjectConfirmationRequest'?即把它放在一個也可以嘲弄的不同類中? – Boklucius