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

Moq access method failed for internal constructors #434

Closed
jdbriaris opened this issue Aug 18, 2017 · 2 comments
Closed

Moq access method failed for internal constructors #434

jdbriaris opened this issue Aug 18, 2017 · 2 comments

Comments

@jdbriaris
Copy link

I've been attempting to use moq to mock a class with an internal constructor. Following on from my SO post i appear to be hitting the constructor, but now faced with a new error, i.e.,

System.MethodAccessException
Attempt by method 'Castle.Proxies.MyClassProxy..ctor(Castle.DynamicProxy.IInterceptor[])'
to access method 'MyAssembly.MyClass..ctor()' failed.
at Castle.Proxies.StageProxy..ctor(IInterceptor[] )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] >arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List1 proxyArguments, Type classToProxy, Object[] constructorArguments) 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) at Moq.Mock1.b__20_0()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock1.InitializeInstance() at Moq.Mock1.OnGetObject()
at Moq.Mock`1.get_Object()
at TestAssembly.UnitTest1.TestMethod1() in c:\users\briaris-j\documents\visual studio 2017\Projects\TestMoqInternals\TestAssembly\UnitTest1.cs:line 16

This is my AssemblyInfo.cs for MyAssembly, i.e., the assembly containing my internal types

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MyAssembly")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MyAssembly")]
[assembly: AssemblyCopyright("Copyright ©  2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2fda4fb1-6855-4af5-a0a3-fbe861dcc734")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Allow moq to access internal constructors of entities
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

Any ideas how this may be fixed?

@jdbriaris
Copy link
Author

jdbriaris commented Aug 18, 2017

Using [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")] seems to work. Why does the version with the PublicKey as stated in the docs not work?

@stakx
Copy link
Contributor

stakx commented Aug 20, 2017

@jdbriaris, thank you for reporting this.

The quickstart guide contained an inaccuracy here. You only need to include the public key in the assembly name if your own assembly is strong-named. If it isn't, then you should not include the public key.

It seems that you are not strong-naming your project that uses Moq, so I believe you've already discovered the correct solution.

I've updated the quickstart guide accordingly.

@stakx stakx closed this as completed Aug 20, 2017
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

2 participants