6

私はMoqに完全に慣れていないので、 System.Reflection.Assemblyクラスのモックを作成しようとしています。私はこのコードを使用しています:

var mockAssembly = new Mock<Assembly>(); 
mockAssembly.Setup( x => x.GetTypes() ).Returns( new Type[] { 
    typeof( Type1 ), 
    typeof( Type2 ) 
} );

しかし、テストを実行すると、次の例外が発生します。

System.ArgumentException : The type System.Reflection.Assembly 
implements ISerializable, but failed to provide a deserialization 
constructor 
Stack Trace: 
   at 
Castle.DynamicProxy.Generators.BaseProxyGenerator.VerifyIfBaseImplementsGet­ObjectData(Type 
baseType) 
   at 
Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] 
interfaces, ProxyGenerationOptions options) 
   at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type 
classToProxy, Type[] additionalInterfacesToProxy, 
ProxyGenerationOptions options) 
   at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type 
classToProxy, Type[] additionalInterfacesToProxy, 
ProxyGenerationOptions options, Object[] constructorArguments, 
IInterceptor[] interceptors) 
   at Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor 
interceptor, Type[] interfaces, Object[] arguments) 
   at Moq.Mock`1.<InitializeInstance>b__0() 
   at Moq.PexProtector.Invoke(Action action) 
   at Moq.Mock`1.InitializeInstance() 
   at Moq.Mock`1.OnGetObject() 
   at Moq.Mock`1.get_Object() 

MoqでISerializableクラス(のような)をモックする正しい方法を教えてください。System.Reflection.Assembly

前もって感謝します!

4

5 に答える 5

2

この問題は、ISerializable インターフェイスにはありません。ISerializable クラスをモックできます

例外メッセージに注意してください。

System.Reflection.Assembly 型は ISerializable を実装していますが、逆シリアル化コンストラクターを提供できませんでした

問題は、Assembly が逆シリアル化コンストラクターを提供していないことです。

于 2010-04-23T10:14:40.837 に答える
1

モックの代わりに、動的アセンブリを作成して、そこから構築することができます。

var appDomain = AppDomain.CurrentDomain;
var assembly = appDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.ReflectionOnly);
于 2010-05-14T19:40:14.067 に答える