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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:12+00:00 2026-05-26T16:41:12+00:00

I ran into some strange behavior when attempting to simplify the creation of a

  • 0

I ran into some strange behavior when attempting to simplify the creation of a rather complex expression tree for Setup/Verify matching with moq.

Assume I am mocking the simple interface defined below

public interface IService
{
    int Send(int value);
}

The following code represents 5 tests. One test for each of the mockSender.Setup(...). Can anyone explain why the tests marked as failing do fail?

[Test]
public void TestInlineSetup()
{
    const int expected = 5;
    var mockSender = new Mock<IService>(MockBehavior.Loose);

    //passes
    mockSender.Setup(s => s.Send(It.IsAny<int>())).Returns(expected);

    //fails
    var sendMatch = It.IsAny<int>();
    mockSender.Setup(s => s.Send(sendMatch)).Returns(expected);

    //passes
    mockSender.Setup(s => s.Send(SendMatchFromMethod())).Returns(expected);

    //fails
    var sendMatch = SendMatchFromMethod();
    mockSender.Setup(s => s.Send(sendMatch)).Returns(expected);

    //fails (this is somewhat contrived, but I have reasons for wanting to curry this)
    mockSender.Setup(s => s.Send(SendMatchFromCurriedMethod()())).Returns(expected);

    Assert.That(mockSender.Object.Send(expected), Is.EqualTo(expected));
}

public static int SendMatchFromMethod()
{
    return It.IsAny<int>();
}

public static Func<int> SendMatchFromCurriedMethod()
{
    return () => It.IsAny<int>();
}

Edit: I know about Mock.Of<..>(..) and normally prefer to use it but in this case it is not an option.

  • 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-26T16:41:13+00:00Added an answer on May 26, 2026 at 4:41 pm

    The problem stems from the way Moq tries to parse the supplied expression tree to create a parameter matcher. You can find the source here:-

    http://code.google.com/p/moq/source/browse/trunk/Source/MatcherFactory.cs

    Referring to the source:-

    • It.IsAny<int> matchers are detected by compiling and executing the expression that is passed as the parameter and looking for any matches (see here).
    • The above step only takes place for method calls and member accesses

    So with that in mind….

    1. Second test fails because the It.IsAny<int> method has been evaluated outside of the matcher factory. As such you have a MemberAccess expression to 0.
    2. Third test passes because SendMatchFromMethod is treated as a method call expression and the call is evaluated inside the MatcherFactory.
    3. Fourth test fails for the same reason as the second, the function has already evaluated and Moq can’t detect you’ve actually called It.Is<Any>
    4. Fifth test fails because the Expression is treated as a Function Invocation and Moq does not perform the matcher check for this type of expression.

    The fourth test, to be honest, should pass and it just seems an oversight that its been left off, probably just because it’s a bit of an edge case.

    Finally Match.Create<T> or MatchAttribute can be used to deal with complex predicates, perhaps they might fit your use case?

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

Sidebar

Related Questions

I'm working with a nullable DateTime object and ran into some strange behavior. Here's
I'm playing around with jQuery Mobile and ran into some (for me) strange behavior.
I've just started with opengl but I ran into some strange behaviour. Below I
(this is regarding the Ramaze.net framework) I ran into some really strange problems while
I've ran into a strange situation. I'm writing some test cases for my program.
I just ran into some unexpected behavior with DateTime.UtcNow while doing some unit tests.
We ran into some strange results recently in one of our Perl scripts, where
I just ran into some code that overuse semicolons, or use semicolon for different
I was doing some performance metrics and I ran into something that seems quite
I'm trying to create some HtmlHelper extensions and ran into a bit of a

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.