Trying to verify a method is being called withi开发者_运维知识库n a mock but cna't seem to figure out why I keep getting an exception thrown that it isn't being called. The method in question is as follows:
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();
}
}
I've then written a test like so:
[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();
}
When I run it the test fails with the message I specified in the Verify(). So I set a bunch of breakpoints and I see that the test never calls the syncEngine.SyncPickLists() method which is confusing to me. So I comment out the two Verify() & VerifyAll() statements and now I see things being called correctly. What am I missing here? It looks like I'm doing exactly what is shown in the Quickstart as well as on this thread.
Answer posted by Tejs in comment to original post.
You call Verify after the call to SyncPickLists - it's not a deferred thing, as it checks the value then and there. Move the line below your call to SyncPickLists and you should pass. – Tejs
精彩评论