NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Not possible to Substitute.For(new[] { typeof(ISomething) }, null) in 4.3.0

Open appel1 opened this issue 3 years ago • 4 comments

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;

appel1 avatar Mar 03 '22 10:03 appel1

Had missed that there already was a pull request for this: https://github.com/nsubstitute/NSubstitute/pull/683

appel1 avatar Mar 03 '22 12:03 appel1

This is also the cause of https://github.com/MRCollective/AutofacContrib.NSubstitute/issues/65

AlexVallat avatar May 04 '22 09:05 AlexVallat

NSubstitute 4.4.0 should include the fix for this. Could you please confirm?

dtchepak avatar Jul 10 '22 07:07 dtchepak

Upgraded to v4.4.0 and ran all my tests; I'm not able to reproduce this issue anymore.

rcdailey avatar Aug 23 '22 22:08 rcdailey

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 :)

304NotModified avatar Apr 29 '24 12:04 304NotModified