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

Arg.Any<T>() behaves strangely when it's stored in a variable instead of being passed directly #743

Open
urnie opened this issue Oct 18, 2023 · 0 comments

Comments

@urnie
Copy link

urnie commented Oct 18, 2023

Describe the bug
The effect of Arg.Any<T>() depends on whether it's stored in a variable or passed directly.

To Reproduce

    public interface INumberChecker
    {
        public bool Exists(int num);
    }

    public class MyApp
    {
        private readonly INumberChecker _numberChecker;
        public MyApp(INumberChecker myInterfaceImpl)
        {
            _numberChecker = myInterfaceImpl;
        }
        public void CheckIfNumberExists(int num)
        {
            _numberChecker.Exists(num);
        }
    }

    [Fact]
    public void MinimalReproducibleExample()
    {
        var sub = Substitute.For<INumberChecker>();

        var anyInt = Arg.Any<int>(); //storing this as a variable here..

        sub.Exists(anyInt).Returns(true); //..and using it here

        var app = new MyApp(sub);
        app.CheckIfNumberExists(1);

        sub.DidNotReceive().Exists(anyInt); //..and here
    }

Expected behaviour
With the given code, I'd expect the test to fail, since sub does receive a call to its Exists(). However, the test passes.

However, if I pass Arg.Any<int>() directly to the methods it's used in, I get expected behaviour (i.e. an error saying that Exists() was called):

    [Fact]
    public void MinimalReproducibleExample()
    {
        var sub = Substitute.For<INumberChecker>();

        sub.Exists(Arg.Any<int>()).Returns(true); //return value doesn't matter here

        var app = new MyApp(sub);
        app.CheckIfNumberExists(1);

        sub.DidNotReceive().Exists(Arg.Any<int>());
    }

Environment:

  • NSubstitute version: [5.1.0]
  • NSubstitute.Analyzers version: [CSharp 1.0.16]
  • Platform: [dotnet 6.0 on Windows, XUnit]

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant