问题描述
在RhinoMocks的,你可以告诉你的嘲弄来IgnoreArguments作为一个毯子声明。在起订量,现在看来,你必须指定It.IsAny()为每一个参数。然而,这并不对ref和out参数工作。如何测试下面的方法,我需要MOQ内部服务调用返回一个特定的结果:
In RhinoMocks, you can just tell your mocks to IgnoreArguments as a blanket statement. In Moq, it seems, you have to specify It.IsAny() for each argument. However, this doesn't work for ref and out arguments. How can I test the following method where I need to Moq the internal service call to return a specific result:
public void MyMethod() {
// DoStuff
IList<SomeObject> errors = new List<SomeObject>();
var result = _service.DoSomething(ref errors, ref param1, param2);
// Do more stuff
}
测试方法:
Test method:
public void TestOfMyMethod() {
// Setup
var moqService = new Mock<IMyService>();
IList<String> errors;
var model = new MyModel();
// This returns null, presumably becuase "errors"
// here does not refer to the same object as "errors" in MyMethod
moqService.Setup(t => t.DoSomething(ref errors, ref model, It.IsAny<SomeType>()).
Returns(new OtherType()));
}
更新:那么,从裁判改变错误走出去工程。因此,它看起来像真正的问题是有,你不能注入ref参数。
UPDATE: So, changing errors from "ref" to "out" works. So it seems like the real issue is having a ref parameter that you can't inject.
推荐答案
正如你已经想通了问题是你的 REF
参数。
As you already figured out the problem is with your ref
argument.
起订量目前仅支持精确匹配为 REF
参数,这意味着只调用如果你通过相同的实例您在设置
用什么相匹配。所以没有一般匹配So It.IsAny()
将无法工作。
Moq currently only support exact matching for ref
arguments, which means the call only matches if you pass the same instance what you've used in the Setup
. So there is no general matching so It.IsAny()
won't work.
请参阅起订量的
// ref arguments
var instance = new Bar();
// Only matches if the ref argument to the invocation is the same instance
mock.Setup(foo => foo.Submit(ref instance)).Returns(true);
和起订量的:
参考匹配意味着,如果该方法是$设置只匹配b调用它与同一实例$ b。 It.IsAny返回null,所以大概
不是你要找的东西。
使用相同的实例中设置为一个在实际通话和
设置将匹配。
Use the same instance in the setup as the one in the actual call, and the setup will match.
这篇关于如何使起订量忽略的ref或out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!