NSubstitute
NSubstitute copied to clipboard
IEnumerable<T> Argument Matchers behave incorrectly when T is an enum and the argument is a casted array
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<IntEnum>> "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