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

I have a layout created like this:

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

Intellisense for the installation method says the following:

"Defines a mocked type setting for calling the void return method.

But the mocking method p.GetBytes () does not return void, it returns an array of bytes.

Alternatively, another installation method is defined as Setup <>, and I can create my layout, for example:

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

The intellisense of this installation method states:

"Sets the mocked type to call the value return method.

.
.
Whatever method I choose, it compiles and checks OK. So, I am confused how I should do it. What is the difference between .Setup () and .Setup <> (), and am I doing it right?

Documents for Moka are a little lacking, let's say so. :)

+6
source share
1 answer

The compiler infers from the lambda passed to Setup() that you intended to call the generic version, and therefore it joyfully passes the generic argument to you. If you use Reflector, you will see that the first code sample actually calls Setup<byte[]>() .

+8
source

Source: https://habr.com/ru/post/893205/


All Articles