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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:52:21+00:00 2026-06-08T14:52:21+00:00

I have come across is situation when mocking a method with an output parameter

  • 0

I have come across is situation when mocking a method with an output parameter using NSubstitute.
I’m not sure how best to explain it in text, so I’ll use some contrived examples and test cases…

In this contrived example, I’ll be using a NSubstitute mock of a IDictionary<string, string>.

private static IDictionary<string, string> GetSubstituteDictionary()
{
    IDictionary<string, string> dict = Substitute.For<IDictionary<string, string>>();

    string s;
    dict.TryGetValue("key", out s).Returns(ci => { ci[1] = "value"; return true; });

    return dict;
}

Now, when I use this mock object in a simple manner, it returns as expected:

[Test]
public void ExampleOne()
{
    var dict = GetSubstituteDictionary();

    string value;
    bool result = dict.TryGetValue("key", out value);

    Assert.That(result, Is.True); // this assert passes.
    Assert.That(value, Is.EqualTo("value")); // this assert passes.
}

However, when I call the same code in a for loop, I get some unexpected behaviour:

[Test]
public void ExampleTwo()
{
    var dict = GetSubstituteDictionary();

    for (int i = 0; i < 2; i++)
    {
        string value;
        bool result = dict.TryGetValue("key", out value);

        Assert.That(result, Is.True); // this assert FAILS - unexpected!
        Assert.That(value, Is.EqualTo("value")); // this assert still passes.
    }
}

In particular, the Assert.That(result, Is.True); assertion passes on the first iteration of the loop, but fails on the second (and any subsequent) iteration.

However, if I modify the string value; line to be string value = null;, the assertion passes for all iterations.

What is causing this anomaly? Is this due to some semantics of C# for loops that I am missing, or is it an issue with the NSubstitute library?

  • 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-08T14:52:23+00:00Added an answer on June 8, 2026 at 2:52 pm

    The reason is that the value variable changes in the loop (set via the output parameter), so that it no longer matches the call you stubbed out.

    You can try using .ReturnsForAnyArgs(), although you’ll need to check the key within the stub then rather than via an argument matcher.

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

Sidebar

Related Questions

I have come across a situation I have not encountered before using the IF
I have come across a situation today which has me wondering about best practices.
I have come across a situation (which I think is weird but is possibly
I have come across many articles which warn against using links to provide logout
I'm making a big C project and I have never come across a situation
I have come across a situation where it would be better to represent a
I have come across this situation. In the hash1 first column is the key
I have come across an awkward situation where I would like to have a
I often come across the situation where I have lots of lines folded and
I have come across a situation that conflicts with my current understanding of methods

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.