2010-04-23 28 views
5

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.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() 

Ô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!

Trả lời

2

Sự cố không có giao diện ISerializable. Bạn có thể các lớp ISerializable giả.

Thông báo thông điệp ngoại lệ:

Loại System.Reflection.Assembly thực hiện ISerializable, nhưng thất bại để cung cấp một deserialization constructor

Vấn đề là, rằng hội không cung cấp deserialization constructor.

+0

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

+0

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: | –

+0

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

1

Thay vì giả lập, bạn có thể thử tạo một cụm động và xây dựng từ đó.

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

Như đã giải thích, vấn đề là Assembly không trưng ra hàm tạo deserialization. Điều này không có nghĩa là nó không thể được thực hiện mặc dù.

Một giải pháp sử dụng Moq theo ví dụ của bạn sẽ là:

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

Lưu ý rằng để sử dụng _Assembly bạn sẽ cần phải tham khảo System.Runtime.InteropServices

4

System.Reflection.Assembly là trừu tượng, vì vậy bạn không thể tạo một phiên bản mới của nó. Tuy nhiên, bạn có thể tạo một lớp thử nghiệm và thực hiện điều đó.

Ví dụ:

 
[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 } }

+1

+1 Sử dụng RhinoMocks, thủ thuật '_Assembly' không hiệu quả với tôi, và tôi đã sử dụng nó. Chỉ tìm kiếm một câu hỏi để chia sẻ kiến ​​thức này. – mao47

Các vấn đề liên quan