Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:29:06+00:00 2026-05-23T22:29:06+00:00

I have a mock being created like this: var mock = new Mock<IPacket>(MockBehavior.Strict); mock.Setup(p

  • 0

I have a mock being created like this:

var mock = new Mock<IPacket>(MockBehavior.Strict);
mock.Setup(p => p.GetBytes()).Returns(new byte[] { }).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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:29:07+00:00Added an answer on May 23, 2026 at 10:29 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to mock objects, but I understand that I need to have my
I have a mock as below: MockRepository mocks = new MockRepository(); ILoanRepository loanRepo =
I have a method that contains an asynchronous call like this: public void MyMethod()
I have the following setup code: MockOf<IObjectSet<Dummy>>().Setup(c => c.AddObject(dummy)).Verifiable(); MockOf<IObjectContextWrapper>().Setup(c => c.GetObjectSet<Dummy>()).Returns(MockOf<IObjectSet<Dummy>>().Object); where Dummy
I have a pretty simple setup for this unit test. I have a class
I have a basic understanding of mock and fake objects, but I'm not sure
I have a unit test that creates a mock calls my method to be
I'm working on a media library mock-up and I have a database of songs
I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code
I need to mock a GrailsControllerClass interface. Instance should have a static variable defined.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.