我试图嘲弄我的仓库的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进行比较,我仍然得到空值而不是交易摘要我要返回。有任何想法吗?
谢谢,当我更新我的代码到'_mockRepository.Setup(r => r.Get(It.IsAny>>()))。返回(new DealSummary {FileName = “测试”});'测试通过。 –
shuniar