Substitute for an internal interface

2,252 views
Skip to first unread message

Rudi

unread,
Mar 1, 2012, 5:54:10 AM3/1/12
to NSubstitute
Hi, when I try to create
var repo = Substitute.For<IRepository>();
where IRepository is defined as an internal interface in the assembly
under test, I get the following error:
Castle.DynamicProxy.Generators.GeneratorException: Type
SomeNamespace.IRepository is not visible to DynamicProxy. Can not
create proxy for types that are not accessible. Make the type public,
or internal and mark your assembly with [assembly:
InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]
attribute.
at Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(Type
target)
at
Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type
interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type
interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, IInterceptor[] interceptors)
at
NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.CreateProxyUsingCastleProxyGenerator(Type
typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments, IInterceptor interceptor, ProxyGenerationOptions
proxyGenerationOptions)
at
NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.GenerateProxy(ICallRouter
callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments)
at NSubstitute.Proxies.ProxyFactory.GenerateProxy(ICallRouter
callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments)
at NSubstitute.Core.SubstituteFactory.Create(Type[] typesToProxy,
Object[] constructorArguments)
at NSubstitute.Substitute.For(Type[] typesToProxy, Object[]
constructorArguments)
at NSubstitute.Substitute.For[T](Object[] constructorArguments)

The way to possibly solve it: http://groups.google.com/group/moqdisc/browse_thread/thread/d40c0ab145f77be9
It seems that the 'InternalsVisible.ToDynamicProxyGenAssembly2' class
and constant are not 'merged' into the NSubstitute assembly.

If I then add the 'ridiculously long' name in manually, I still get
this error:

System.TypeLoadException: Type 'Castle.Proxies.IRepositoryProxy' from
assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' is attempting to implement an inaccessible
interface.
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule
module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CreateType(TypeBuilder
type)
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
at
Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String
typeName, Type proxyTargetType, Type[] interfaces, INamingScope
namingScope)
at
Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__DisplayClass1.<GenerateCode>b__0(String
n, INamingScope s)
at
Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey
cacheKey, Func`3 factory)
at
Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type
proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at
Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type
interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type
interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, IInterceptor[] interceptors)
at
NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.CreateProxyUsingCastleProxyGenerator(Type
typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments, IInterceptor interceptor, ProxyGenerationOptions
proxyGenerationOptions)
at
NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.GenerateProxy(ICallRouter
callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments)
at NSubstitute.Proxies.ProxyFactory.GenerateProxy(ICallRouter
callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[]
constructorArguments)
at NSubstitute.Core.SubstituteFactory.Create(Type[] typesToProxy,
Object[] constructorArguments)
at NSubstitute.Substitute.For(Type[] typesToProxy, Object[]
constructorArguments)
at NSubstitute.Substitute.For[T](Object[] constructorArguments)

Any ideas, if this could/should/would be supported?

David Tchepak

unread,
Mar 1, 2012, 6:07:28 PM3/1/12
to nsubs...@googlegroups.com
Thanks for reporting this. I've raised a bug here: https://github.com/nsubstitute/NSubstitute/issues/69

It may be (wild guess here) because we are ilmerging and internalising DynamicProxy.

I'm not sure when I'll get a chance to dig into this issue. Are you able to work around it for now?


--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.


Rudi

unread,
Mar 2, 2012, 11:18:59 AM3/2/12
to NSubstitute
Yes, for now I have set the interface to be public. So not an
immediate blocking issue.
> >http://groups.google.com/group/moqdisc/browse_thread/thread/d40c0ab14...
> > It seems that the 'InternalsVisible.ToDynamicProxyGenAssembly2' class
> > and constant are not 'merged' into the NSubstitute assembly.
>
> > If I then add the 'ridiculously long' name in manually, I still get
> > this error:
>
> > System.TypeLoadException: Type 'Castle.Proxies.IRepositoryProxy' from
> > assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
> > PublicKeyToken=null' is attempting to implement an inaccessible
> > interface.
> >   at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule
> > module, Int32 tk, ObjectHandleOnStack type)
> >   at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
> >   at System.Reflection.Emit.TypeBuilder.CreateType()
> >   at
>
> > Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CreateType(Type Builder
> > type)
> >   at
> > Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
> >   at
>
> > Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.Generat eType(String
> > typeName, Type proxyTargetType, Type[] interfaces, INamingScope
> > namingScope)
> >   at
>
> > Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__Displ ayClass1.<GenerateCode>b__0(String
> > n, INamingScope s)
> >   at
> > Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey
> > cacheKey, Func`3 factory)
> >   at
>
> > Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCo de(Type
> > proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
> >   at
>
> > Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarg et(Type
> > interfaceToProxy, Type[] additionalInterfacesToProxy,
> > ProxyGenerationOptions options)
> >   at
> > Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type
> > interfaceToProxy, Type[] additionalInterfacesToProxy,
> > ProxyGenerationOptions options, IInterceptor[] interceptors)
> >   at
>
> > NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.CreateProx yUsingCastleProxyGenerator(Type
> > typeToProxy, Type[] additionalInterfaces, Object[]
> > constructorArguments, IInterceptor interceptor, ProxyGenerationOptions
> > proxyGenerationOptions)
> >   at
>
> > NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.GeneratePr oxy(ICallRouter

David Tchepak

unread,
Mar 3, 2012, 3:47:46 AM3/3/12
to nsubs...@googlegroups.com
Hi Rudi, 

I ended up getting it working for my test case by specifying the dynamic proxy generated DLL name without key info:

[assembly:InternalsVisibleTo("LibraryUnderTest.Tests")]
[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")] 

https://github.com/nsubstitute/NSubstitute/issues/69#issuecomment-4299342

Can you please check if this works for your case? (I had to do a clean then build to make sure the assembly updated properly after changing AssemblyInfo.cs)

Rudi

unread,
Mar 5, 2012, 6:00:36 AM3/5/12
to nsubs...@googlegroups.com
Yes, works as expected.
 
Thanks!

On Saturday, March 3, 2012 9:47:46 AM UTC+1, David Tchepak wrote:
Hi Rudi, 

I ended up getting it working for my test case by specifying the dynamic proxy generated DLL name without key info:

[assembly:InternalsVisibleTo("LibraryUnderTest.Tests")]
[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")] 

https://github.com/nsubstitute/NSubstitute/issues/69#issuecomment-4299342

Can you please check if this works for your case? (I had to do a clean then build to make sure the assembly updated properly after changing AssemblyInfo.cs)


> > For more options, visit this group at
> >http://groups.google.com/group/nsubstitute?hl=en.

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages