diff --git a/src/NSubstitute/Core/Extensions.cs b/src/NSubstitute/Core/Extensions.cs index 608d98fa..592e7673 100644 --- a/src/NSubstitute/Core/Extensions.cs +++ b/src/NSubstitute/Core/Extensions.cs @@ -36,8 +36,9 @@ public static bool IsCompatibleWith(this object? instance, Type type) aCompatibleInstanceType.GetGenericTypeDefinition() == genericTypeDefinition) { // both are the same generic type. If their GenericTypeArguments match then they are equivalent - return CallSpecification.TypesAreAllEquivalent( - aCompatibleInstanceType.GenericTypeArguments, type.GenericTypeArguments); + var typesAreAllEquivalent = CallSpecification.TypesAreAllEquivalent(aCompatibleInstanceType.GenericTypeArguments, type.GenericTypeArguments); + if (typesAreAllEquivalent) + return true; } } } diff --git a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs index 19e3c99c..2a38bb77 100644 --- a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs +++ b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs @@ -784,6 +784,24 @@ public void Supports_matching_custom_class_with_derived_class_argument() service.Received().MyMethod(Arg.Any()); } + [Test] + public void Supports_matching_custom_with_multiple_generic_arguments_string() + { + var service = Substitute.For(); + var argument = new MyStringIntArgument(); + + service.MyMethodWithReturnValue().Returns(argument); + } + + [Test] + public void Supports_matching_custom_with_multiple_generic_arguments_int() + { + var service = Substitute.For(); + var argument = new MyStringIntArgument(); + + service.MyMethodWithReturnValue().Returns(argument); + } + [Test] public void Supports_matching_generic_interface_bound_type_ArgAnyType_with_class_argument() { @@ -837,6 +855,9 @@ public void SetUp() public interface IMyService { void MyMethod(IMyArgument argument); + + IMyArgument MyMethodWithReturnValue(); + } public interface IMyArgument { } @@ -845,6 +866,7 @@ public class MyStringArgument : IMyArgument { } public class MyOtherStringArgument : IMyArgument { } public class MySampleClassArgument : IMyArgument { } public class MyOtherSampleClassArgument : IMyArgument { } + public class MyStringIntArgument : IMyArgument, IMyArgument { } public class MySampleDerivedClassArgument : MySampleClassArgument { } [Test]