试图验证一个方法是在一个模拟器中被调用,但似乎没有弄清楚为什么我一直得到一个异常抛出它没有被调用。有问题的方法如下:
public class CustomerSyncEngine {
public CustomerSyncEngine(ILoggingProvider loggingProvider, ICrmProvider crmProvider, ICacheProvider cacheProvider) {
Logger = loggingProvider;
CrmProvider = crmProvider;
CacheProvider = cacheProvider;
}
public virtual void SyncPickLists() {
Logger.LogBeginPicklistSync();
// get all the pick lists from the local cache
var localCachePickLists = CacheProvider.GetPickLists().ToList();
// get all the pick lists from the remote system
var crmPickLists = CrmProvider.GetPickLists().ToList();
// build a sync plan
var changes = BuildPickListUpdatePlan(localCachePickLists, crmPickLists).ToList();
// run the sync
RunPickListSync(changes);
Logger.LogEndPicklistSync();
}
}
然后我写了一个类似的测试:
[TestMethod]
public void TestSyncPickLists() {
// arrange
var mockCrm = new Mock<ICrmProvider>();
mockCrm.Verify(x => x.GetPickLists(), Times.Once(), "ICrmProvider.GetPickLists not called");
var mockCache = Mock.Of<ICacheProvider>();
var mockLogger = Mock.Of<ILoggingProvider>();
// act
var syncEngine = new CustomerSyncEngine(mockLogger, mockCrm.Object, mockCache);
syncEngine.SyncPickLists();
// assert
mockCrm.VerifyAll();
}
当我运行它时,测试失败并显示我在Verify()中指定的消息。所以我设置了一堆断点,我看到测试从不调用syncEngine.SyncPickLists()方法,这让我很困惑。所以我评论了两个Verify()&amp; VerifyAll()语句,现在我看到正确调用的东西。我在这里错过了什么?看起来我正在完成Quickstart以及this thread所显示的内容。
答案 0 :(得分:0)
Tejs在对原帖发表评论时发表的答案。
在调用SyncPickLists之后调用Verify - 它不是延迟的东西,因为它会在那里检查值。将调用下方的行移至SyncPickLists,您应该通过。 - Tejs