0
試圖驗證方法是在模擬內部調用的,但cna't似乎找不到調用它的原因。有問題的方法如下:使用Moq調用方法的驗證方法
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()& VerifyAll()語句,現在我看到了正確調用的東西。我在這裏錯過了什麼?看起來我正在執行Quickstart以及this thread中顯示的內容。
在調用'SyncPickLists'後調用'Verify' - 它不是延遲的事情,因爲它會在那裏檢查值。將呼叫下方的行移至'SyncPickLists',您應該通過。 – Tejs
太棒了......修好了! –