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

IEnumerable<T> Argument Matchers behave incorrectly when T is an enum and the argument is a casted array #847

Open
uflowie opened this issue Nov 29, 2024 · 0 comments
Labels
bug Reported problem with NSubstitute behaviour

Comments

@uflowie
Copy link

uflowie commented Nov 29, 2024

Describe the bug
When substituting a method that takes in an argument of type IEnumerable<T> where T is an enum, passing in casted arrays returns incorrect results. This is the case for both Arg.Any and directly passing the array in the setup. In the example below we expect the Subsitute to return "Hello" in both cases, but an empty string is returned instead. In Versions <=5.1 these Argument Matchers behave correctly.

To Reproduce

 [Fact]
 public void ArgAnyFails()
 {
     int[] ints = [1, 2, 3];
     IEnumerable<IntEnum> enums = (IEnumerable<IntEnum>)(IEnumerable<int>)ints;

     var testObject = Substitute.For<ITest>();
     testObject.TestMethod(Arg.Any<IEnumerable<IntEnum>>()).Returns("Hello");
     var result = testObject.TestMethod(enums);
     
     Assert.Equal("Hello", result); // fails
 }

 [Fact]
 public void DirectArgFails()
 {
     int[] ints = [1, 2, 3];
     IEnumerable<IntEnum> enums = (IEnumerable<IntEnum>)(IEnumerable<int>)ints;

     var testObject = Substitute.For<ITest>();
     testObject.TestMethod(enums).Returns("Hello");
     var result = testObject.TestMethod(enums);
     
     Assert.Equal("Hello", result); // fails
 }

 public enum IntEnum;

 public interface ITest
 {
     string TestMethod(IEnumerable<IntEnum> enums);
 }

Expected behaviour
In the case of Arg.Any<IEnumerable> "Hello" should always be returned. Even passing null will return "Hello".
In the case of passing the object that the Subsitute was set up for, "Hello" should be returned.

Environment:

  • NSubstitute version: 5.3. In Versions <=5.1 these Argument Matchers behave correctly.
  • Platform: dotnet8 on Windows
@dtchepak dtchepak added the bug Reported problem with NSubstitute behaviour label Dec 8, 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

2 participants