Skip to content

Mocked out parameter value is stuck inspite of calling CallRouter.Clear(ClearOptions.All) #589

Closed
@davidnemeti

Description

@davidnemeti

Describe the bug

After mocking an out parameter value either either with Returns or with When-Do, the mocked out parameter value is stuck inspite of calling CallRouter.Clear(ClearOptions.All).

I believe this might be happen because when the out parameter value is being set, then it goes directly into the Call._arguments, where it stays forever despite the CallRouter.Clear(ClearOptions.All) call, until another Returns or When-Do sets the out parameter value to something else.

To Reproduce

void Main()
{
    var foo = Substitute.For<IFoo>();

    int number1, number2;

    Console.WriteLine("Empty substitute");

    foo.Baz(out number1);
    foo.Bar(out number2);

    Console.WriteLine(number1);
    Console.WriteLine(number2);

    foo
        .Baz(out Arg.Any<int>())
        .Returns(callInfo =>
        {
            Console.WriteLine("Inside 'Returns'");
            callInfo[0] = 5;
            return 21;
        });

    foo
        .When(x => x.Bar(out Arg.Any<int>()))
        .Do(callInfo =>
        {
            Console.WriteLine("Inside 'Do'");
            callInfo[0] = 8;
        });

    Console.WriteLine();
    Console.WriteLine("Mocked substitute");

    foo.Baz(out number1);
    foo.Bar(out number2);

    Console.WriteLine(number1);
    Console.WriteLine(number2);

    var callRouter = SubstitutionContext.Current.GetCallRouterFor(foo);
    callRouter.Clear(ClearOptions.All);

    Console.WriteLine();
    Console.WriteLine("Cleared substitute");

    foo.Baz(out number1);
    foo.Bar(out number2);

    Console.WriteLine(number1);
    Console.WriteLine(number2);
}

public interface IFoo
{
    void Bar(out int number);
    int Baz(out int number);
}

The above code writes this to the console:

Empty substitute
0
0

Mocked substitute
Inside 'Returns'
Inside 'Do'
5
8

Cleared substitute
5
8

Expected behaviour

The above code should write this to the console:

Empty substitute
0
0

Mocked substitute
Inside 'Returns'
Inside 'Do'
5
8

Cleared substitute
0
0

Environment

  • NSubstitute version: 4.2.1
  • Platform: .NET Framework 4.8 on Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugReported problem with NSubstitute behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions