这有点奇怪。我正在尝试存根一个没有参数的方法,我不关心参数是什么,所以我忽略了参数。它看起来像这样:
List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
.Returns(something);
当像这样调用存根方法时,这不会出现任何问题:
List<Foo> target;
var result = service.Method(out target);
但是,当 target
预初始化时,它不起作用。例如:
List<Foo> target = new List<Foo>();
var result = service.Method(out target);
当我检查假货上的 Tag
时,我可以看到输出参数被记录为
所以我怀疑它们在以下情况下不匹配out 目标已经设置为某个值。我尝试将测试中的 ignored
设置为 new List()
并尝试了 A>.Ignored 但两者都对结果没有任何影响。
所以我的问题是,如果输出参数目标已经有值,有谁知道如何在没有输出参数的情况下存根方法?
This is a bit of an odd one. I'm trying to stub a method which has out parameters, I don't care about what the parameters are so I'm ignoring the arguments. It looks like this:
List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
.Returns(something);
This works without any problems when the stubbed method is called like so:
List<Foo> target;
var result = service.Method(out target);
However, it doesn't work when the target
is pre-initialised. For example:
List<Foo> target = new List<Foo>();
var result = service.Method(out target);
When I inspect the Tag
on the fake, I can see that the out parameters are being recorded as <NULL>
so I suspect they're not matching when the out target is already set to something. I've tried setting the ignored
in my test to new List<Foo>()
and also tried A<List<Foo>>.Ignored
but neither has any effect on the result.
So my question is, does anyone know how to stub a method with out parameters if the out parameter target already has a value?
发布评论
评论(1)
更新:自FakeItEasy 1.23.0起<的初始值code>out 参数在匹配时被忽略,因此不需要
WithAnyArguments
,五分钟后,我找到了一个可接受的解决方案(在这种情况)。由于我对传递给此方法的参数不感兴趣,因此如果我使用
WithAnyArguments()
方法,那么它似乎可以工作;我想,这必须简化参数检查的过程。最终代码是:
如果我不想忽略所有参数,这显然不能解决问题。如果没有人有更复杂的解决方案,我只会接受这个答案。
Update: since FakeItEasy 1.23.0 the initial value of
out
parameters is ignored when matching, so no need forWithAnyArguments
, Five minutes later and I've found an acceptable solution (in this scenario). As I'm not interested in what arguments are passed to this method, so if I use the
WithAnyArguments()
method then it seems to work; this must shortcut the argument checking all together, I guess.The final code is:
This obviously doesn't solve the problem if I don't want to ignore all the arguments. I'll only accept this answer if nobody has a more sophisticated solution.