1

I took a method from this post (accepted answer code) that serializes object so that I can get a new copy of that object.

Now I am trying to do a unit test but everytime I gets to the object that I am trying to clone it comes up with

Tests.ServiceLayer.TestTService.Test_Something: System.Runtime.Serialization.SerializationException : Type 'Moq.Interceptor' in Assembly 'Moq, Version=3.1.416.3, Culture=neutral, PublicKeyToken=69f491c39445e920' is not marked as serializable.

So it seems to be that my mocked up object has some stuff in it that the clone is trying to copy but can't.

I am not unsure how to tell it to ignore these things. I would mock out the clone but I am using an extension method that I don't think I can mock out.

Community
  • 1
  • 1
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • What are you trying to test? The mock will most likely not be serializable (as it indeed is not) and therefore the Clone method will not work for it. But then I would imagine you only want to copy simple data objects and therefore you should be able to create a simple Stub rather than a mock from Moq<> – mfloryan Feb 11 '11 at 11:02
  • @mfloryan - well since this object is a domain object that is used with nHibernate it has many object references to other objects. So it is actually a deep copy that is needed. I can create the task manually but it is just alot of leg work since I got to create many of the collection of objects within that object. So mocking up most of it was kinda nice. If I can't get around it I will do this. – chobo2 Feb 11 '11 at 17:38

1 Answers1

0

The proxy class created by Moq is not Serializable and that's why you can't use the Clone method in this case. You must either replace the mocked object with something else or try to mock the extension method as per this blog post.

mfloryan
  • 7,667
  • 4
  • 31
  • 44
  • @ mfloryan- I am not sure if I should do what they did in that post. like what I am trying to clone is a domain object that is used with nhibernate too. So it is all just virtual properties. I don't know there is a point to make a interface out of that. – chobo2 Feb 11 '11 at 18:42
  • OK. I get that. But if the object is really 'deep' (lots of collections and stuff) than creating its clone can potentially be very costly and since NHibernate is creating proxies anyway (that's why the properties are virtual) - I don't see much point in doing a deep copy on the proxies. What is the point of the clone? – mfloryan Feb 13 '11 at 23:38
  • I have a view that is strongly typed to a view model. Once the view model is sent to the server and validated I use auto mapper to map it to a domain model(all properties that may link to other domain models and nhibe..). I have option in my view to create a copy of what they just created. I need to do a deep clone of this domain model so that I can make a couple changes to the copy(like start date of an appointment as it repeats every week so the copy needs to have a start date 7 more days added to original start date). I then take all these clones and the original and save to db. – chobo2 Feb 14 '11 at 00:37