Skip to content

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

@uflowie

Description

@uflowie

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

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