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

7

System.Reflection.Assembly 是抽象的,因此您不能创建它的新实例。但是,您可以创建一个测试类并模拟它。

例子:

[测试方法]
公共无效 TestSomethingThatNeedsAMockAssembly()
{
    字符串标题=“标题”;
var mockAssembly = new Mock();
mockAssembly.Setup(a => a.GetCustomAttributes(It.Is(s => s == Type.GetType("System.Reflection.AssemblyTitleAttribute")), It.IsAny())).Returns(new System.Attribute[ ] { 新的 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 } }

于 2013-09-10T20:20:35.310 回答
2

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

注意异常消息:

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

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

于 2010-04-23T10:14:40.837 回答
2

如前所述,问题在于 Assembly 没有公开反序列化构造函数。但这并不意味着它不能完成。

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

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

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

于 2013-07-10T01:04:34.753 回答
1

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

var appDomain = AppDomain.CurrentDomain;
var assembly = appDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.ReflectionOnly);
于 2010-05-14T19:40:14.067 回答
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);
        }
于 2019-03-06T14:33:46.167 回答