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

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

Open
rholek opened this issue Dec 12, 2024 · 0 comments · May be fixed by #858
Labels
bug Reported problem with NSubstitute behaviour

Comments

@rholek
Copy link

rholek commented Dec 12, 2024

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.

rholek pushed a commit to rholek/NSubstitute that referenced this issue Jan 6, 2025
@304NotModified 304NotModified added the bug Reported problem with NSubstitute behaviour label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reported problem with NSubstitute behaviour
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants