Ich habe versucht, Moq zu verwenden, um eine Klasse mit einem internen Konstruktor zu verspotten. Im Anschluss an meinem früheren post i erscheinen den Konstruktor zu schlagen, aber jetzt mit einem neuen Fehler konfrontiert wird, das heißt,Moq-Zugriffsmethode für interne Konstruktoren fehlgeschlagen
System.MethodAccessException Versuch nach der Methode ‚Castle.Proxies.MyClassProxy..ctor (Castle.DynamicProxy .Ilnterceptor []) ' Zugriff auf die Methode' MyAssembly.MyClass..ctor() 'fehlgeschlagen. bei Castle.Proxies.StageProxy..ctor (IInterceptor []) --- Ende des inneren Ausnahme Stack-Trace --- bei System.RuntimeMethodHandle.InvokeMethod (Objekt Ziel, Objekt []> Argumente, Signatur sig, boolescher Konstruktor) bei System.Reflection.RuntimeConstructorInfo.Invoke (BindingFlags InvokeAttr, Binder Binder, Object [] Parameter, CultureInfo Kultur) bei System.RuntimeType.CreateInstanceImpl (BindingFlags BindingAttr, Binder Binder, Object [] args, CultureInfo Kultur, Objekt [] activationAttributes, StackCrawlMark & stackMark) bei System.Activator.CreateInstance (Typ type, BindingFlags bindingAttr, Sammelmappe, Object [] args, CultureInfo-Kultur, Object [] activationAttributes) bei System.Activator.CreateInstance (Typ Typ, Objekt [] Argumente) bei Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance (Typ proxytype, List
1 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.Mock
1.b__20_0() bei Moq.PexProtector.Invoke (Aktion Aktion) bei Moq.Mock1.InitializeInstance() at Moq.Mock
1.OnGetObject() bei Moq.Mock`1. get_Object() bei TestAssembly.UnitTest1.TestMethod1() in c: \ Benutzer \ briaris-j \ Dokumente Visual Studio 2017 \ Projects \ \ TestMoqInternals \ TestAssembly \ UnitTest1.cs: Linie 16
Das ist mein AssemblyInfo.cs
für MyAssembly
, dh die Baugruppe mit meinen internen Typen
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")]
Irgendwelche Ideen, wie dies behoben werden kann?
Haben Sie das Attribut 'InternalsVisibleTo' hinzufügen? – CodeNotFound
Ja, ich habe hinzugefügt: '[assembly: InternalsVisibleTo ("DynamicProxyGenAssembly2, PublicKey = 0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]' gemäß dem [docs] (https://github.com/Moq/moq4/wiki/Quickstart#advanced-features) –
Seltsam. Ihr Fehler besagt, dass der Versuch, auf den Konstruktor zuzugreifen, fehlgeschlagen ist, und ich denke, Sie haben das Attribut nicht der richtigen Assembly zugewiesen. – CodeNotFound