Skip to content

ReturnsForAnyArgs and WhenForAnyArgs and generic arguments #152

@alexandrnikitin

Description

@alexandrnikitin

This is more a question rather than a bug report. Inspired by a question on SO
Suppose we have an interface with a generic method:

public interface IInterfaceWithGenericMethod
{
    int GenericMethod<T>(T arg);
}

Then when we specify the sub to return a value for any argument then it fails on the call with another generic type.

var sub = Substitute.For<IInterfaceWithGenericMethod>();

const int expectedInt = 1;
sub.GenericMethod(Arg.Any<int>()).ReturnsForAnyArgs(expectedInt); // Specifying for int

Assert.AreEqual(expectedInt, sub.GenericMethod(0)); // Works for int
Assert.AreEqual(expectedInt, sub.GenericMethod("")); // Fails for string

The same is right for WhenForAnyArgs().
Imo this isn't obvious behavior. Thoughts?

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