Skip to content

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

@gianvitodifilippo

Description

@gianvitodifilippo

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion on NSubstitute behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions