2011-08-18 41 views
1

我試圖嘲弄我的倉庫的get()方法,以便在該對象上返回一個對象,以僞造的更新,但我的設置是不工作:起訂量SetUp.Return不工作的倉庫模擬

這裏我的測試:

[Test] 
public void TestUploadDealSummaryReportUploadedExistingUpdatesSuccessfully() 
{ 
    var dealSummary = new DealSummary {FileName = "Test"}; 
    _mockRepository.Setup(r => r.Get(x => x.FileName == dealSummary.FileName)) 
     .Returns(new DealSummary {FileName = "Test"}); //not working for some reason... 

    var reportUploader = new ReportUploader(_mockUnitOfWork.Object, _mockRepository.Object); 
    reportUploader.UploadDealSummaryReport(dealSummary, "", ""); 

    _mockRepository.Verify(r => r.Update(dealSummary)); 
    _mockUnitOfWork.Verify(uow => uow.Save()); 
} 

這裏是正在測試的方法:

public void UploadDealSummaryReport(DealSummary dealSummary, string uploadedBy, string comments) 
{ 
    dealSummary.UploadedBy = uploadedBy; 
    dealSummary.Comments = comments; 

    // method should be mocked to return a deal summary but returns null 
    var existingDealSummary = _repository.Get(x => x.FileName == dealSummary.FileName); 
    if (existingDealSummary == null) 
     _repository.Insert(dealSummary); 
    else 
     _repository.Update(dealSummary); 

    _unitOfWork.Save(); 
} 

這裏是當我運行我的單元測試,我得到的錯誤:

Moq.MockException: 上模擬預期調用至少一次,但從未進行的:R => r.Update(.dealSummary) 沒有配置設置。

演出調用: IRepository 1.Get(x => (x.FileName == value(FRSDashboard.Lib.Concrete.ReportUploader+<>c__DisplayClass0).dealSummary.FileName)) IRepository 1.Insert(FRSDashboard.Data.Entities.DealSummary) 在Moq.Mock.ThrowVerifyException(MethodCall預期,IEnumerable的1 setups, IEnumerable 1 actualCalls,表達表達,倍,的Int32 callCount) 在Moq的(模擬模擬,表達式1 expression, Times times, String failMessage) at Moq.Mock)1.在FRSDashboard.Test.FRSDashboard.Lib.ReportUploaderTest上驗證(表達式1表達式) .TestUploadDealSummaryReportUploadedExistingUpdatesSuccessfully

通過調試,我發現x => x.FileName返回null,但即使我將它與null進行比較,我仍然得到空值而不是交易摘要我要返回。有任何想法嗎?

回答

7

我猜你的設置與您所做的調用不匹配,因爲它們是兩個不同的匿名lambdas。你可能需要像

_mockRepository.Setup(r => r.Get(It.IsAny<**whatever your get lambda is defined as**>()).Returns(new DealSummary {FileName = "Test"}); 

您可以通過在你的倉庫的get()方法設置斷點,如果被擊中看到驗證。它不應該。

+0

謝謝,當我更新我的代碼到'_mockRepository.Setup(r => r.Get(It.IsAny >>()))。返回(new DealSummary {FileName = 「測試」});'測試通過。 – shuniar