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

  • SEARCH
  • Home
  • 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 9138533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:16:08+00:00 2026-06-17T09:16:08+00:00

The mocking library Moq has a Setup() method with the signature: public ISetup<T, TResult>

  • 0

The mocking library Moq has a Setup() method with the signature:

public ISetup<T, TResult> Setup<TResult>(Expression<Func<T, TResult>> expression)

So I can do something like this just fine (using the library’s recursive mocking):

Mock<Thing> _thing = new Mock<Thing>();
_thing.Setup((Thing t) => t.PropA.SubPropB).Returns(string.Empty);

But this fails:

Expression<Func<Thing, object>> test = (Thing t) => t.PropA.SubPropB;
_thing.Setup(test).Returns(string.Empty);

with the error:

Expression is not a method invocation: t => (Object)t.PropA.SubPropB

What’s the difference between the inlined lambda and the one assigned to a variable first? Aren’t both expression trees and not yet compiled (Moq parses the tree)?

Edit – Looks like the issue is with the Func<Thing, object> typing. Why is, e.g., string acceptable, but object is not?

  • 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-17T09:16:09+00:00Added an answer on June 17, 2026 at 9:16 am

    Why is, e.g., string acceptable, but object is not?

    because function declaration in moq is not covariant by generic types. Try to Setup moq in next way

     _thing.Setup(test).Returns((object)string.Empty);
    

    because you have next signature Expression<Func<Thing, object>> and it’s not covariant with respect to object

    Or change your signature to string like this (assume t.PropA.SubPropB returns string):

    Expression<Func<Thing, string>> test = (Thing t) => t.PropA.SubPropB;
                            //^here should be string
    

    Real-case
    I have created a test project with Expression as local variable, it all works fine with string and object. Please check my configuration if I missed something. Moq – 4.0.10827v

    [TestFixture]
    public class Class1
    {
        [Test]
        public void TestMethod()
        {
            Mock<Thing> _thing = new Mock<Thing>();
    
            Expression<Func<Thing, string>> setup = t => t.PropA.SubPropB;
    //                               ^ works with string and object
            _thing.Setup(setup).Returns(string.Empty);
    
            Assert.IsEmpty(_thing.Object.PropA.SubPropB);
    
        }
    }
    
    public class Thing
    {
        public virtual Thingy PropA { get; set; }
    }
    
    public class Thingy
    {
        public virtual string SubPropB { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm mocking a method with easymock that has a date in its body, something
I'm using Moq library. I'm mocking up an instance that does all regular CRUD
I'm trying to mock an method to a third-party library using moq. The problem
New to Moq and Mocking in general. Testing a class that has a generic
I am mocking in this sort of situation: class A { public IB B
For quickly mocking up UI, I'd like to be able to drag buttons onto
I have seen many posts and questions about Mocking a private method but still
Mocking sealed classes can be quite a pain. I currently favor an Adapter pattern
I am currently unit testing my code with a mocking library. I am now
In mocking frameworks, you can mock an object or spy on it. What's the

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.