-
Notifications
You must be signed in to change notification settings - Fork 266
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
Unable to mock interfaces that consume value types by reference #378
Comments
This isn't an NSubstitute-specific problem. I found the same problem with public class TempSpec
{
// Fails.
[Fact]
internal void Struct_ByRef_Moq_Test()
{
_ = Mock.Of<IStructByRefConsumer>();
}
// Fails.
[Fact]
internal void Struct_ByRef_NSubstitute_Test()
{
_ = Substitute.For<IStructByRefConsumer>();
}
// Passes.
[Fact]
internal void Struct_ByValue_Moq_Test()
{
_ = Mock.Of<IStructByValueConsumer>();
}
// Passes.
[Fact]
internal void Struct_ByValue_NSubstitute_Test()
{
_ = Substitute.For<IStructByValueConsumer>();
}
}
Both of these fail at: Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors) So I guess the bug is in Castle.Core. |
@TAGC Thanks for firing the issue for Castle. It's indeed their issue 😉 |
Just to let you guys know, the Castle.Core team are considering bumping their library to (at least) .NET Standard 1.5 in order to resolve this proxy generation issue. The Moq guys are prepared to do something similar when the Castle.Core team release an updated build, so you may want to consider following suit. |
Thanks a lot for the update! Let's re-open this issue to track the process.
That should not be a big problem, as we are working on the major version now, so it's a good time to make this change. |
Required list of actions:
|
This should address nsubstitute#378. Castle.Core release notes: https://github.com/castleproject/Core/releases/tag/v4.3.0
Hello Today I noticed the same issue again with a different setup: Minimal example: using NSubstitute;
using NUnit.Framework;
namespace DebugNUnitError
{
[TestFixture]
public class Class1
{
public interface IBar {}
public interface IFoo<T>
{
bool DoSomething(in string[] stringArray);
}
[Test]
public void TestSubstituteFor()
{
var mock = Substitute.For<IFoo<IBar>>();
}
}
} Library and runtime versions:
As far as I understood the issue should be fixed since Core 2.2.2 and in the current version of NSubstitute. But I still get this error:
Further information:
|
@Netzeband I think this is related to castleproject/Core#430. It still seems it is not fixed in current CoreCLR versions (castleproject/Core#430 (comment)). |
@dtchepak Thanks for the hint. This seems to be the same issue. So I will watch the other ticket to see, if it got fixed or not. |
C# 7.2 introduced the possibility of using reference semantics for value types - this is done by applying the
in
modifier for value-type parameters in method signatures.NSubstitute is apparently not able to generate mocks for interfaces that use these new modifiers. Given the unit tests below:
What I find is that
IStructByValueConsumer_Test
passes as expected, whileIStructByRefConsumer_Test
fails with this exception:The text was updated successfully, but these errors were encountered: