验证使用 Moq 调用的方法
尝试验证一个方法是否在模拟中被调用,但 cna 似乎不明白为什么我不断抛出一个异常,表明它没有被调用。有问题的方法如下:
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() 语句,现在我看到事情被正确调用。我在这里缺少什么?看起来我正在按照快速入门中所示的方式进行操作如此线程 。
Trying to verify a method is being called within 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tejs 在原始帖子的评论中发布的答案。
您在调用 SyncPickLists 之后调用验证 - 这不是延迟的事情,因为它会立即检查值。将调用下方的行移至 SyncPickLists,您应该会通过。 – 特伊斯
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