您能帮我了解Moq回调吗? [英] Can you help me understand Moq Callback?

查看:63
本文介绍了您能帮我了解Moq回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Moq并查看了Callback,但我无法找到一个简单的示例来了解如何使用它.

Using Moq and looked at Callback but I have not been able to find a simple example to understand how to use it.

您是否有一个小的工作片段,清楚地说明了如何以及何时使用它?

Do you have a small working snippet which clearly explain how and when to use it?

推荐答案

很难击败 https: //github.com/Moq/moq4/wiki/快速入门

如果这还不够清楚,我会称其为文档错误...

If that's not clear enough, I'd call that a doc bug...

为您的澄清...

对于您执行的每个模拟方法Setup,您都需要指出以下内容:

For each mocked method Setup you perform, you get to indicate things like:

  • 输入约束
  • 要导出的返回值(如果有)的/方法的值

.Callback机制说:我现在无法描述它,但是当发生这样的呼叫时,请给我回电,然后我将做需要做的事情".作为同一条流畅的调用链的一部分,您可以控制结果通过.Returns返回(如果有).在QS示例中,一个示例是使返回值每次都增加.

The .Callback mechanism says "I can't describe it right now, but when a call shaped like this happens, call me back and I'll do what needs to be done". As part of the same fluent call chain, you get to control the result to return (if any) via .Returns". In the QS examples, an example is that they make the value being returned increase each time.

通常,您将不需要这种机制(xUnit测试模式具有测试中类似的条件逻辑的反模式术语),并且如果有任何更简单或内置的方式来建立所需的条件,应该优先使用它.

In general, you won't need a mechanism like this very often (xUnit Test Patterns have terms for antipatterns of the ilk Conditional Logic In Tests), and if there's any simpler or built-in way to establish what you need, it should be used in preference.

部分Justin Etheredge的Moq系列中的4之3 对此进行了介绍,并

Part 3 of 4 in Justin Etheredge's Moq series covers it, and there's another example of callbacks here

有关回调的简单示例,请参见与Moq一起使用回调

A simple example of a callback can be found at Using Callbacks with Moq post.

这篇关于您能帮我了解Moq回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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