开发者

Moq confusion - Setup() v Setup<>()

开发者 https://www.devze.com 2023-03-22 06:01 出处:网络
I have a mock being created like this: var mock = new Mock<IPacket>(MockBehavior.Strict); mock.Setup(p => p.GetBytes()).Returns(new byte[开发者_JAVA百科] { }).Verifiable();

I have a mock being created like this:

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

The intellisense for the Setup method says this:

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

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

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();

The intellisense of this Setup method states:

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

.

.

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?

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


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[]>().

0

精彩评论

暂无评论...
验证码 换一张
取 消