Skip to content
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

InternalsVisibleTo #258

Closed
ramtinkermani opened this issue May 10, 2016 · 4 comments
Closed

InternalsVisibleTo #258

ramtinkermani opened this issue May 10, 2016 · 4 comments

Comments

@ramtinkermani
Copy link

Moq throws an ArgumentEception when trying to use the mocked method that is Internal to the SUT project, even though the following attribute is set in the main project.

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=002400000..<TRUNCATED>..5cc7")]

Interestingly when I remove the public key, all works fine and no exceptions are thrown. I checked the public key from the source code and it is correct (Originally copied from the Quick Start page, verified later from the source).

Any ideas what is wrong?

Here is the Exception:

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Type to mock must be an interface or an abstract or non-sealed class. 
  Source=Moq
  StackTrace:
       at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
                                                     <TRUNCATED>
       Source=mscorlib
       TypeName=Castle.Proxies.CLASS_TO_MOCKProxy
       StackTrace:
            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.ClassProxyGenerator.GenerateType(String name, Type[] interfaces, INamingScope namingScope)
            at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
            at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
            at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
       InnerException: 

@szogun1987
Copy link

There is an message "Type to mock must be an interface or an abstract or non-sealed class" in exception details. Do thing that you are trying to mock is interface or non-sealed class?

@stakx
Copy link
Contributor

stakx commented Jun 4, 2017

@ramtinkermani, can you please provide a short but complete example code that reproduces this issue?

@stakx
Copy link
Contributor

stakx commented Jun 21, 2017

I am closing this for the moment, since no code for reproducing this issue has been provided so far. @ramtinkermani: Feel free to report back with repro code, we can reopen this issue anytime.

@stakx
Copy link
Contributor

stakx commented Dec 29, 2017

@ramtinkermani - I'm just doing some repo housecleaning and came across this old issue of yours again:

Moq throws an ArgumentEception when trying to use the mocked method that is Internal to the SUT project, even though the following attribute is set in the main project.

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=002400000..<TRUNCATED>..5cc7")]

Interestingly when I remove the public key, all works fine and no exceptions are thrown.

This is due to a somewhat hard-to-understand feature of Castle DynamicProxy. Depending on whether your own client code is signed (i. e. strong-named) or not, it puts generated proxies in a strong-named or weak-named assembly. (Sometimes it even splits generated types across both.) By including or omitting the public key in the above attribute, you're either referring the strong-named or weak-named dynamic assembly.

From the perspective of Moq and DynamicProxy, things work best if you sign your own client code assembly (if that is an option). If you don't want to do that, you can also try including both [assembly: InternalsVisibleTo(...)] attributes with the public key and without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants