9

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?)

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • 1
    Try **immediate** window and output to **debug** while traversing your object. To make your life easier, you can also prepare yourself this traversal method and then call it in immediate window and it would output to debug. And while at it, convert it to JSON and then use that string in your test code to parse it back to concrete instance. From JSON string. Likely the simplest solution. – Robert Koritnik Nov 28 '12 at 17:20
  • But you can of course go for it and write a VS extension that would add context menu item to right-clicking an object in watch window and would say **Put JSON on clipboard**. I'm sure many would be happy to install it. Exactly for the same reason you provided. – Robert Koritnik Nov 28 '12 at 17:26
  • +1 My question EXACTLY. – adamdport Apr 14 '14 at 21:20

1 Answers1

2

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.

Community
  • 1
  • 1
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
  • Cycles can be avoided... but are a bit more memory consuming... ;) – Robert Koritnik Nov 28 '12 at 17:26
  • Avoiding cycles is a complication... but I listed some other things that could make it impossible to create a tool like that. – Miguel Angelo Nov 28 '12 at 17:42
  • 1
    So how come you'll be able to serialize it then? Serialization tools do the whole magic the same way as any custom code should. Be it XML, JSON or any other serialization – Robert Koritnik Nov 28 '12 at 17:47
  • Serialization cannot deal with unconstructible objects... they need some hand-coding to work with objects that are built by a factory for example. – Miguel Angelo Nov 28 '12 at 17:49
  • 2
    Good points. I can see that making a tool that is fool proof would not be possible. But I would be happy if it would script it out as if all Constructors were parameterless and public. If there are complicated constructors then I can go back and add factories or parameters as needed. – Vaccano Nov 28 '12 at 22:07