最小起订量-Setup()v Setup<>() [英] Moq confusion - Setup() v Setup<>()

查看:74
本文介绍了最小起订量-Setup()v Setup<>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的模拟物:

I have a mock being created like this:

var mock = new Mock<IPacket>(MockBehavior.Strict);
mock.Setup(p => p.GetBytes()).Returns(new byte[] { }).Verifiable();

Setup方法的智能提示是这样的:

The intellisense for the Setup method says this:

"为呼叫 void 的模拟类型指定设置 返回方法."

"Specifies a setup on the mocked type for a call to a void returning method."

但是模拟的方法p.GetBytes()不会返回void,而是返回一个字节数组.

But the mocked method p.GetBytes() does not return void, it returns a byte array.

或者,将另一种Setup方法定义为Setup<>,我可以这样创建模拟程序:

Alternatively another Setup method is defined as Setup<>, and I can create my mock like this:

var mock = new Mock<IPacket>(MockBehavior.Strict);
mock.Setup<byte[]>(p => p.GetBytes()).Returns(new byte[] { }).Verifiable();

此Setup方法的智能提示:

The intellisense of this Setup method states:

"为呼叫的模拟类型指定设置 返回方法."

"Specifies a setup on the mocked type for a call to a value returning method."

.
.
无论选择哪种方法,它都可以编译并测试确定.因此,我对应该采用哪种方式感到困惑. .Setup()和.Setup<>()有什么区别,我做对了吗?

.
.
Whichever method I choose, it compiles and tests OK. So, I'm confused as to which way I should be doing this. What is the difference between .Setup() and .Setup<>(), and am I doing it right?

我们应该说缺少Moq的文档. :)

The docs for Moq are somewhat lacking, shall we say. :)

推荐答案

编译器从传递给Setup()的lambda推断您要调用通用版本,因此很高兴为您推断出通用参数.如果使用Reflector,您会看到第一个代码示例实际上是在调用Setup<byte[]>().

The compiler is inferring from the lambda passed to Setup() that you meant to call the generic version, and so it happily infers the generic argument for you. If you use Reflector you will see that the first code example is in fact calling Setup<byte[]>().

这篇关于最小起订量-Setup()v Setup&lt;&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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