nsubstitute / NSubstitute

A friendly substitute for .NET mocking libraries.
https://nsubstitute.github.io
Other
2.65k stars 260 forks source link

Unable to match arguments whose type is generic, when their concrete type is not known #786

Open GianvitoDifilippo opened 6 months ago

GianvitoDifilippo commented 6 months ago

Question Hello everyone. I'm having trouble in verifying a call to a method accepting a single argument, whose type is a generic interface (say, IMyArgument<T>). At compile time, I don't have access to the concrete type of the argument being passed, so I tried Arg.Any<IMyArgument<Arg.AnyType>>(), but that fails.

IMyService service = Substitute.For<IMyService>();
var argument = new MyStringArgument();

service.MyMethod(argument);

// The following line throws. Using ReceivedWithAnyArgs didn't help.
service.Received().MyMethod(Arg.Any<IMyArgument<Arg.AnyType>>());

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

public interface IMyArgument<T> { }

// Suppose I don't have access to this type at compile time, so I could not have written Arg.Any<MyStringArgument>()
public class MyStringArgument : IMyArgument<string> { }

I couldn't find any information addressing this specific issue in the documentation. Any suggestion is appreciated :)

304NotModified commented 4 months ago

Duplicate of https://github.com/nsubstitute/NSubstitute/issues/757?