Tôi hoàn toàn mới đối với Moq và hiện đang cố gắng tạo mô hình cho lớp học System.Reflection.Assembly
. Tôi đang sử dụng mã này:Làm thế nào để giả lập các lớp ISerializable với Moq?
var mockAssembly = new Mock<Assembly>();
mockAssembly.Setup(x => x.GetTypes()).Returns(new Type[] {
typeof(Type1),
typeof(Type2)
});
Nhưng khi tôi chạy thử nghiệm tôi nhận được ngoại lệ tiếp theo:
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()
Ông có thể khuyên bạn nên cho tôi đúng cách để nhạo báng ISerializable
lớp (như System.Reflection.Assembly
) với Moq.
Cảm ơn trước!
Ok, cảm ơn. Nhưng bạn có thể gợi ý làm thế nào để cung cấp constructor deserialization bằng cách sử dụng Moq trong trường hợp đó. – sam
bạn không thể - Lắp ráp không có bất kỳ nhà xây dựng có thể truy cập, và như vậy nó unmockable khi sử dụng Moq: | –
Bạn không cần phải cung cấp một constructor deserialization để giả lắp ráp. Bạn có thể giả lập lớp Interop _Assembly và cast thành Assembly khi cần thiết. – nathanchere