Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MuOuchen opened this issue Nov 1, 2023 · 1 comment
Labels
bug Reported problem with NSubstitute behaviour

Comments

@MuOuchen
Copy link

MuOuchen commented Nov 1, 2023

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]
@MuOuchen MuOuchen changed the title NSubstitute seems not to recognize the parameter order correctly NSubstitute seems not to recognize the parameter order correctly (string default method parameter null) Nov 1, 2023
@Ergamon
Copy link

Ergamon commented Nov 1, 2023

I cant really explain why, but the problem is the "null" in the expectation. Seems NSubstitute cannot figure the right Arg.Is syntax in that case.

If you replace the null with Arg.Is it works:

mock.Received(1).WeirdMethod(Arg.Any<string>(), Arg.Is<string>(v => v == null), Arg.Any<string>(), CancellationToken.None);

@304NotModified 304NotModified added the bug Reported problem with NSubstitute behaviour label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reported problem with NSubstitute behaviour
Projects
None yet
Development

No branches or pull requests

3 participants