我完全是Moq的新手,现在正试图为 System.Reflection.Assembly
类创建一个模拟。我使用此代码:如何用Moq模拟ISerializable类?
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.VerifyIfBaseImplementsGetObjectData(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()
你能reccomend我起订量嘲笑ISerializable
类 (如System.Reflection.Assembly
)的正确途径。
在此先感谢!
好,感谢。但是,你能否建议在这种情况下如何使用Moq提供反序列化构造函数。 – sam 2010-04-23 20:27:17
你不能 - Assembly没有任何可访问的构造函数,因此当使用Moq时它是unmockable:| – 2010-04-24 08:24:37
您不需要提供反序列化构造函数来模拟Assembly。您可以模拟互操作_Assembly类并在需要时转换为Assembly。 – nathanchere 2013-07-10 01:05:41