C# 使用最小起订量的“短路”无效方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1203529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Short circuiting" void methods with Moq?
提问by bakasan
my team has made the decision recently to use Moq as our mocking framework for its tremendous flexibility and highly readable syntax. As we're new to it, I'm stumbling on what appears to be simple questions--searches (here, Google, etc.) find plenty of discussions on other nuances of Moq, but not necessarily what I'm after, and the few seemingly related questions have turned into red herrings.
我的团队最近决定使用 Moq 作为我们的模拟框架,因为它具有极大的灵活性和高度可读的语法。因为我们是新手,所以我遇到了看似简单的问题——搜索(这里,谷歌等)发现了大量关于 Moq 其他细微差别的讨论,但不一定是我所追求的,并且几个看似相关的问题已经变成了红鲱鱼。
We're testing a class that has an external dependency (Amazon SimpleDb to be precise) but don't want our tests bound to having a live connection out. A particular method:
我们正在测试一个具有外部依赖项(准确地说是 Amazon SimpleDb)的类,但不希望我们的测试绑定到实时连接。一个特定的方法:
- Applies some "business" logic
- If appropriate, invokes a call out to SDB via a provider we've built, let's call it
SaveItem()
- 应用一些“业务”逻辑
- 如果合适,通过我们构建的提供程序调用对 SDB 的调用,我们称之为
SaveItem()
I want to unit test this such that we setup the context required and insure that SaveItem()
was invoked, but in a manner that SaveItem()
really isn't invoked (because A) the provider to SDB is a mock that isn't fully hydrated and will likely bomb and B) I don't want to have to pay for that transaction hundreds and thousands of times).
我想对此进行单元测试,以便我们设置所需的上下文并确保它SaveItem()
被调用,但以一种SaveItem()
真正未被调用的方式(因为 A)SDB 的提供者是一个没有完全水合的模拟并且可能会爆炸B)我不想为那笔交易支付成百上千次的费用)。
When dealing with methods that returned a value, this was trivial.
在处理返回值的方法时,这是微不足道的。
mockDb.Setup(d => d.GiveMeSomething()).Returns("Foo");
In the case that I outline above though, my "SaveItem()
" method is void and therefore the option to use Moq's Returns()
method isn't available. And while I can setup a callback to verify SaveItem()
is invoked, I can't however seem to get it to not actually do anything.
在我上面概述的情况下,我的“ SaveItem()
”方法无效,因此使用 MoqReturns()
方法的选项不可用。虽然我可以设置一个回调来验证SaveItem()
被调用,但我似乎无法让它实际上不做任何事情。
Naive/hopeful, I thought the following would work, but it appears to still invoke the method:
天真/充满希望,我认为以下方法可行,但它似乎仍会调用该方法:
mockDb.Setup(d => d.SaveItem(It.IsAny<object>()));
So the million dollar question: What's the Moq of the following fictitious code?
所以百万美元的问题:以下虚构代码的起订量是多少?
mockDb.Setup(d => d.SaveItem(It.IsAny<object>())).STOP_RIGHT_HERE();
采纳答案by womp
If the SaveItem()
method is virtual or abstract, and you're not setting Callbase = true
, then the method should be re-implemented to do nothing by the mock.
如果该SaveItem()
方法是虚拟的或抽象的,并且您没有设置Callbase = true
,则应重新实现该方法,以便模拟不执行任何操作。
You should be able to do:
你应该能够做到:
mockDb.Setup(d => d.SaveItem(It.IsAny<object>())).Verifiable();
... test here ...
mockDb.Verify();