使用 FakeItEasy 中预先存在的值删除参数

发布于 2024-12-28 16:37:03 字数 770 浏览 1 评论 0 原文

这有点奇怪。我正在尝试存根一个没有参数的方法,我不关心参数是什么,所以我忽略了参数。它看起来像这样:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不羁少年 2025-01-04 16:37:03

更新:自FakeItEasy 1.23.0起<的初始值code>out 参数在匹配时被忽略,因此不需要 WithAnyArguments

,五分钟后,我找到了一个可接受的解决方案(在这种情况)。由于我对传递给此方法的参数不感兴趣,因此如果我使用 WithAnyArguments() 方法,那么它似乎可以工作;我想,这必须简化参数检查的过程。

最终代码是:

List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
  .WithAnyArguments()
  .Returns(something);

如果我不想忽略所有参数,这显然不能解决问题。如果没有人有更复杂的解决方案,我只会接受这个答案。

Update: since FakeItEasy 1.23.0 the initial value of out parameters is ignored when matching, so no need for WithAnyArguments

, 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:

List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
  .WithAnyArguments()
  .Returns(something);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文