MRCollective / AutofacContrib.NSubstitute

Automocking container for Autofac using NSubstitute
MIT License
25 stars 24 forks source link

Has NSubstitute 4.3.0 broken this package? #65

Open AlexVallat opened 2 years ago

AlexVallat commented 2 years ago

It looks like the 4.3.0 release of NSubstitute is not compatible. For a minimal repro:

new AutoSubstitute().Resolve<IDisposable>();

This works fine with NSubstitute 4.2.2 but with 4.3.0 throws an exception:

Message: 
Autofac.Core.DependencyResolutionException : An exception was thrown while activating λ:System.Object.
Data:
  ActivatorChain: λ:System.Object

  ----> NSubstitute.Exceptions.SubstituteException : Can not provide constructor arguments when substituting for an interface.

  Stack Trace: 
ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
ResolvePipeline.Invoke(ResolveRequestContext ctxt)
RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
<>c__DisplayClass5_0.<Execute>b__0()
LifetimeScope.CreateSharedInstance(Guid id, Func`1 creator)
LifetimeScope.CreateSharedInstance(Guid primaryId, Nullable`1 qualifyingId, Func`1 creator)
SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
ScopeSelectionMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
ResolvePipeline.Invoke(ResolveRequestContext ctxt)
ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
ResolveOperation.ExecuteOperation(ResolveRequest request)
ResolveOperation.Execute(ResolveRequest request)
LifetimeScope.ResolveComponent(ResolveRequest request)
Container.ResolveComponent(ResolveRequest request)
ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
ResolutionExtensions.Resolve[TService](IComponentContext context, Parameter[] parameters)
AutoSubstitute.Resolve[T](Parameter[] parameters)
Tests.TestAutoSubstitute() line 19
--SubstituteException
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)
<>c__DisplayClass3_0.<RegistrationsFor>b__0(IComponentContext c, IEnumerable`1 p)
<>c__DisplayClass0_0`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p)
DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
DelegateActivator.<ConfigurePipeline>b__2_0(ResolveRequestContext ctxt, Action`1 next)
DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
balbelias commented 2 years ago

Seconded. Downgrade to NSubstitute 4.2.2 (as stated in nuspec) resolved the problem.

AlexVallat commented 2 years ago

This is caused by https://github.com/nsubstitute/NSubstitute/issues/685

Could be fixed by them, or here by changing https://github.com/MRCollective/AutofacContrib.NSubstitute/blob/9e7498b25beb0b917a6dfd2a735c4cbe72d10b72/AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs#L79

to pass Array.Empty<object>() instead of null

304NotModified commented 6 months ago

As https://github.com/nsubstitute/NSubstitute/issues/685 has been fixed, I assume this could be closed.