Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to take an object in the watch window and "Script" it

I have a scenario where I have a fairly complex object that I load from a database.

That object has several nested objects. While I am debugging I find an instance of this object that I would like to use in a unit test. Right now I have to create this object manually. Since it is fairly complex, it takes me a while.

My unit testing time would be better spent if there was a way to tell the watch window to output this variable to a text window (or the clipboard).

It seems all the info needed is in the watch window.

I would not expect it to create using statements or any such thing, just use the class info it has and create the new statements (nested as many levels as my object goes).

Is there any such tool out there? (If not maybe I just found a way to make my fortune?)

like image 893
Vaccano Avatar asked Nov 28 '12 17:11

Vaccano


1 Answers

There is no such tool that I know... it is very complicated to do this because:

  • objects may have cyclic references, and therefore have no limit to the depth you can go
  • there could be references to singleton objects
  • there could be references to objects that take parameter on the constructor, how could it know how to construct the object?
  • or references to objects that have no public constructors, and are built by a factory instead
  • or references to COM objects
  • or references to objects that make sense only while running: file streams for example

One easy solution: make the object serializable (to xml, or json for example), serialize it, copy the serialized string to your unit test, and then deserialize it in the unit test.

Not so easy solution: implement a debugger visualizer, with a visualizer object source: Debugger Visualizer and "Type is not marked as serializable"

That way you can create a window, and show the serialized object... you will probably have to use reflection to read all object properties, and child objects, and so on.

like image 51
Miguel Angelo Avatar answered Oct 16 '22 04:10

Miguel Angelo