Skip to content

Commit

Permalink
Arg.Any<Arg.AnyType>() does not match arguments passed by reference (#…
Browse files Browse the repository at this point in the history
…787) (#811)

Co-authored-by: Mihnea Rădulescu <>
  • Loading branch information
mihnea-radulescu committed May 14, 2024
1 parent 2cf5aad commit da6c174
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/NSubstitute/Core/CallSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ internal static bool TypesAreAllEquivalent(Type[] aArgs, Type[] bArgs)
if (first.IsGenericType && second.IsGenericType
&& first.GetGenericTypeDefinition() == second.GetGenericTypeDefinition())
{
// both are the same generic type. If their GenericTypeArguments match then they are equivalent
// both are the same generic type. If their GenericTypeArguments match then they are equivalent
if (!TypesAreAllEquivalent(first.GenericTypeArguments, second.GenericTypeArguments))
{
return false;
}
continue;
}

var areEquivalent = first.IsAssignableFrom(second) || second.IsAssignableFrom(first) ||
typeof(Arg.AnyType).IsAssignableFrom(first) || typeof(Arg.AnyType).IsAssignableFrom(second);
var areAssignable = first.IsAssignableFrom(second) || second.IsAssignableFrom(first);
var areAnyTypeAssignable = typeof(Arg.AnyType).IsAssignableFrom(first) ||
typeof(Arg.AnyType).IsAssignableFrom(second);
var areByRefAnyTypeAssignable = first.IsByRef && second.IsByRef &&
(typeof(Arg.AnyType).IsAssignableFrom(first.GetElementType()) ||
typeof(Arg.AnyType).IsAssignableFrom(second.GetElementType()));
var areEquivalent = areAssignable || areAnyTypeAssignable || areByRefAnyTypeAssignable;
if (!areEquivalent) return false;
}
return true;
Expand Down
17 changes: 17 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ReceivedCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ public void Throw_when_negative_min_range_given()
StringAssert.Contains("minInclusive must be >= 0, but was -1.", ex.Message);
}

[Test]
public void Works_with_byref_generic_parameters()
{
IMyService service = Substitute.For<IMyService>();
MyArgument arg = new();
service.MyMethod(ref arg);

service.Received().MyMethod(ref Arg.Any<Arg.AnyType>());
}

public interface ICar
{
void Start();
Expand All @@ -329,4 +339,11 @@ public interface ICar
float GetCapacityInLitres();
event Action Started;
}

public interface IMyService
{
void MyMethod<T>(ref T argument);
}

public class MyArgument { }
}

0 comments on commit da6c174

Please sign in to comment.