如何使用Moq模拟ISerializable类?

问题描述 投票:6回答:5

我是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)的正确方法吗?

提前致谢!

.net moq serializable
5个回答
2
投票

问题不在于ISerializable接口。您可以模拟ISerializable类。

请注意异常消息:

System.Reflection.Assembly类型实现ISerializable,但未能提供反序列化构造函数

问题是,Assembly没有提供反序列化构造函数。


5
投票

System.Reflection.Assembly是抽象的,因此您无法创建它的新实例。但是,你可以创建一个测试类,然后模拟它。

例:

[TestMethod]
public void TestSomethingThatNeedsAMockAssembly()
{
    string title = "title";
var mockAssembly = new Mock();
mockAssembly.Setup(a => a.GetCustomAttributes(It.Is(s => s == Type.GetType("System.Reflection.AssemblyTitleAttribute")), It.IsAny())).Returns(new System.Attribute[] { new AssemblyTitleAttribute(title) } ); var c = new ClassThatTakesAssemblyAndParsesIt(mockAssembly.Object); Assert.IsTrue(c.AssemblyTitle == title); //etc } public class TestAssembly : Assembly { public TestAssembly() { //could probably do something interesting here } }


2
投票

正如已经解释的那样,问题是Assembly没有公开反序列化构造函数。这并不意味着它无法完成。

根据您的示例使用Moq的解决方案是:

    var mock = new Mock<_Assembly>();
    result.Setup(/* do whatever here as usual*/);

请注意,要使用_Assembly,您需要参考System.Runtime.InteropServices


1
投票

您可以尝试创建动态程序集并从中构建,而不是模拟。

var appDomain = AppDomain.CurrentDomain;
var assembly = appDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.ReflectionOnly);

1
投票

我只需要验证嵌入式资源是否正常工作;这对我的情况有用:

    public class MockableAssembly : Assembly { }

    [TestClass]
    public class ApplicationSetupTests
    {
        [TestMethod]
        public void ApplyAsposeLicense_Success()
        {
            var mockAssembly = new Mock<MockableAssembly>();
            mockAssembly.Setup(a => a.GetManifestResourceStream(It.IsAny<string>())).Returns(mockedData);

            MethodIAmTesting(mockAssembly.Object);

            mockAssembly.Verify(a => a.GetManifestResourceStream("myString"), Times.Once);
        }
© www.soinside.com 2019 - 2024. All rights reserved.