Moq中Verifiable()的目的是什么? [英] What is the purpose of Verifiable() in Moq?

查看:84
本文介绍了Moq中Verifiable()的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Verifiable()的目的是什么?

如果我验证了Mock并保留了该内容,它仍然会验证SetUp.

If I verify a Mock and leave this out it still verifies the SetUp.

编辑:我使用的是VerifyAll(),因此所有内容均已通过验证的原因.更改为Verify()后,仅检查了我的.Verifiable() SetUp.

I was using VerifyAll() thus the reason for everything being verified. After changing to Verify() only my .Verifiable() SetUps were being checked.

推荐答案

附录:如其他答案所述,.Verifiable的目的是将Setup纳入一组延迟的Verify(...)调用"中,该调用然后可以通过mock.Verify()触发.

ADDENDUM: As the other answer states, the purpose of .Verifiable is to enlist a Setup into a set of "deferred Verify(...) calls" which can then be triggered via mock.Verify().

OP的澄清清楚地表明这是目标,唯一的问题是弄清楚为什么它不起作用,但是正如@Liam劝说的那样,答案也应该确实涉及到这一点:-据我所知,关键的用例是:

The OP's clarification makes it clear that this was the goal and the only problem was figuring out why it wasn't working, but as @Liam prodded, the answer should really touch on this too:- The key use cases as far as I can see are:

  • 保持mock.Setup()mock.Verify
  • 之间的干燥度
  • 允许从实际的Verify调用本身中断开验证的配置(例如,您可以使用另一种帮助方法进行设置)
  • maintaining DRYness between a mock.Setup() and mock.Verify
  • allowing one to disconnect the configuring of a verification from the actual Verify call itself (e.g., you could set it up in another helper method)

...然后回到我的回答,简洁地说:要小心,因为上述优点通常被认为不利于实现这些目标对测试的易读性和可维护性的影响,构造"

... and back to my answer, which tersely effectively says "be careful as the above pros are commonly considered to be outweighed by the effect that achieving those goals has on the legibility and maintainability of tests which lean too much on such constructs"

原文:请注意,在可能的情况下,应该改用 AAA 布局,因此应该在工作完成后进行明确的mock.Verify( expression )调用,而不是尽可能与mock.Verify()mock.VerifyAll() 配对(来源: @kzu ).

ORIGINAL: Note that where possible, one should instead follow the AAA layout and hence one should be doing explicit mock.Verify( expression ) calls after the work has been done, rather than a mock.Setup( ... ).Verifiable() paired with a mock.Verify() or mock.VerifyAll() wherever possible (credit: @kzu).

这篇关于Moq中Verifiable()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆