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 9272869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:57:53+00:00 2026-06-18T15:57:53+00:00

Currently I’m using Moq to create a handful of mock objects; thus far everything

  • 0

Currently

I’m using Moq to create a handful of mock objects; thus far everything is working nicely. Currently to ‘assign’ a delegate using Moq I’m doing

var someMock = new Mock<ISomeInterface>();
someMock.Setup(x => x.DoSomething(It.IsAny<int>())).Returns(this.DoSomething)

Where this.DoSomething is a method accepting an int parameter; fundamentally it’s the same structure as x.DoSomething on the ISomeInterface.

Question

Is it possible to simply assign a delegate, without the neeed for specifying all of its parameters, i.e. not using It.IsAny<int>()? Ideally something like this:

var someMock = new Mock<ISomeInterface>();
someMock.Setup(x => x.DoSomething).Returns(this.DoSomething)
  • 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-06-18T15:57:54+00:00Added an answer on June 18, 2026 at 3:57 pm

    No, that’s not possible. That’s not a “shortcoming” of Moq – C# doesn’t support it.

    Some background:

    Let’s assume ISomeInterface is declared as follows:

    public interface ISomeInterface
    {
        void Foo(int a, int b);
    }
    

    That would mean that the parameter of the Setup method would have to be a Func<ISomeInterface, Action<int, int>>.
    The problem now is that the Setup method would have to be defined in a generic way, because your method could have any type of parameters:

    Setup<T1, T2>(Func<T, Action<T1, T2>> param)
    

    T is the generic type from the Mock class, T1 and T2 are the parameters of the method.

    Calling this method however will result in a compiler error:

    The type arguments for method Mock.Setup<T1, T2>(System.Func<UserQuery.ISomeInterface,System.Action<T1,T2>>) cannot be inferred from the usage. Try specifying the type arguments explicitly.

    To make it work, you would need to call it like this:

    someMock.Setup<int, int>(x => x.DoSomething)
    

    or like this:

    someMock.Setup(x => (Action<int, int>)x.DoSomething);
    

    In both cases, you would have to specify the type of the parameters, just as you do it now already.

    As to why you get the compiler error:

    x.DoSomething is a method group. There exists an implicit conversion to Action<int, int>.
    However, for this implicit conversion to be executed, the compiler needs to know the types of T1 and T2. But those types can only be inferred after the conversion took place.
    Those two steps depend on each other and that’s why it doesn’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently everything is working golden except for the fact that a user could manually
Currently I am working in message compose screen in Android, Using Intent to show
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently, I'm working on a project where I have a server - client relationship
Currently working with converting SQLException error messages into messages that are more useful for
Currently, I'm working with a company that has several different projects, all running on
Currently, I am using the following command to change dos2unix line endings, but this
Currently, I'm working on to get a list of all running applications. I've been
currently i am working on asp.net mvc where by mistake i have right clicked

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.