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

Arg.Any<Arg.AnyType>() does not match arguments passed by reference (#787) #811

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { }
}
Loading