Skip to content

When seting up a return values with a conrete type that implements generic interface multiple times, the order in which the interfaces are specified is important #853

@rholek

Description

@rholek

Describe the bug
When a type implements generic interface multiple times, the order of specified types is important.

To Reproduce

var mock = Substitute.For<IMock>();
mock.Test.Returns(new StringInt());//this works
mock.Test.Returns(new IntString());//this fails

public interface IMock
{
    IMyInterface<string> Test { get; }
}

public interface IMyInterface<T>;

class IntString : IMyInterface<int>, IMyInterface<string>;

class StringInt : IMyInterface<string>, IMyInterface<int>;

Expected behaviour
The Returns should work regardless of the order of IMyInterface specification

Environment:

  • NSubstitute version: 5.3.0 (worked in 5.1.0)

Additional context
I believe the issue was introduced in commit 73818a6

See

foreach (var aCompatibleInstanceType in compatibleInstanceTypes)
{
	if (aCompatibleInstanceType.IsGenericType &&
		aCompatibleInstanceType.GetGenericTypeDefinition() == genericTypeDefinition)
	{
		// both are the same generic type. If their GenericTypeArguments match then they are equivalent
		return CallSpecification.TypesAreAllEquivalent(
			aCompatibleInstanceType.GenericTypeArguments, type.GenericTypeArguments);
	}
}

Shouldn't there is something like "if true return true else continue"? Though I didn't study the code further, so I am not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugReported problem with NSubstitute behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions