NSubstitute
NSubstitute copied to clipboard
Not possible to Substitute.For(new[] { typeof(ISomething) }, null) in 4.3.0
https://github.com/nsubstitute/NSubstitute/blob/f890efaeb7cebe01aa6304422fd2936dcfefe290/src/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs#L167
It was changed from:
private static bool HasItems<T>(T[]? array)
{
return array != null && array.Length > 0;
}
to
private static bool HasItems<T>(T[]? array) => array?.Length != 0;
Those two statements behave differently if array is null. The old code resulted in false but the new code returns true in this case. I don't think that was an intentional change?
Message:
NSubstitute.Exceptions.SubstituteException : Can not provide constructor arguments when substituting for an interface.
Stack Trace:
CastleDynamicProxyFactory.VerifyNoConstructorArgumentsGivenForInterface(Object[] constructorArguments)
CastleDynamicProxyFactory.CreateProxyUsingCastleProxyGenerator(Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments, IInterceptor[] interceptors, ProxyGenerationOptions proxyGenerationOptions)
CastleDynamicProxyFactory.GenerateTypeProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments)
CastleDynamicProxyFactory.GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments)
SubstituteFactory.Create(Type[] typesToProxy, Object[] constructorArguments, Boolean callBaseByDefault)
SubstituteFactory.Create(Type[] typesToProxy, Object[] constructorArguments)
Substitute.For(Type[] typesToProxy, Object[] constructorArguments)
Changing it to this should fix it:
private static bool HasItems<T>(T[]? array) => array?.Length > 0;
Had missed that there already was a pull request for this: https://github.com/nsubstitute/NSubstitute/pull/683
This is also the cause of https://github.com/MRCollective/AutofacContrib.NSubstitute/issues/65
NSubstitute 4.4.0 should include the fix for this. Could you please confirm?
Upgraded to v4.4.0 and ran all my tests; I'm not able to reproduce this issue anymore.
Upgraded to v4.4.0 and ran all my tests; I'm not able to reproduce this issue anymore.
Great! Then I will close this as done :)