Skip to content

NSubstitute seems not to recognize the parameter order correctly (string default method parameter null) #753

@MuOuchen

Description

@MuOuchen

Describe the bug
I get the following exception, the expected does not fit my argument definition:

    NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
	WeirdMethod(any String, any String, <null>, System.Threading.CancellationToken)
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
	WeirdMethod("1", <null>, *"3"*, System.Threading.CancellationToken)

To Reproduce

Try this test for example:

using NSubstitute;
using NUnit.Framework;

[TestFixture]
public class TestClass
{


    public class ObjectWithWeirdMethod
    {

        public virtual string WeirdMethod(string s1, string s2 = null, string s3 = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return "weird";
        }
    }


    public class UnderTest
    {
        private ObjectWithWeirdMethod _obj;
    
        public UnderTest(ObjectWithWeirdMethod obj) {
            _obj = obj;
        }

        public void test()
        {
            _obj.WeirdMethod("1", null, "3");
        }
    
    }

    [Test]
    public void test()
    {
        //given
        ObjectWithWeirdMethod mock = Substitute.For<ObjectWithWeirdMethod>();
        UnderTest underTest = new UnderTest(mock);
    
        var val = "test";
        mock.WeirdMethod(Arg.Any<string>(), null, Arg.Any<string>(), CancellationToken.None).Returns(val);

        //when
        underTest.test();
        
        //then
        mock.Received(1).WeirdMethod(Arg.Any<string>(), null, Arg.Any<string>(), CancellationToken.None);
    }

}

Expected behaviour
The matched argument should be:

(Arg.Any<string>(), <null>, Arg.Any<string>(), System.Threading.CancellationToken)

Environment:

  • NSubstitute version: [5.1.0]
  • dotnetcore version: 7.0.400 on Windows 10 machine
  • NSubstitute.Analyzers version: [CSharp 1.0.16]

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