2

I've inherited a program containing a very large class file of some 6,000 lines of unruly and totally undocumented code. I'm starting the refactoring process by creating unit tests, but I'm having trouble figuring out the best way to quickly create an instance of this class for testing. Tens of thousands of instances are made of this class when the application is launched, but instantiating it by hand isn't really practical since it has hundreds of attributes and the way that these instances are being constructed is not at all transparent to me. Is there a way I can grab an instance of the class in Visual Studio during debugging to somehow put it in a unit test?

I played around with using this Object Exporter extension, but making this very large and complicated class serializable/deserializable is proving to be it's own can of worms. Are there any other suggestions people have for quickly getting started with unit testing an enormous class file?

  • 5
    Write a static method to serialize the class (XML, Binary, JSON) and call that method in the Immediate window of the debugger. – D Stanley Jun 17 '15 at 21:23
  • I'm afraid if your class can't be serialized or deserialized, you won't be able to just rehydrate it from some kind of saved state. Your best bet is probably to continue trying to understand how the system is constructing instances, and try to leverage that same mechanism. – StriplingWarrior Jun 17 '15 at 21:24
  • Great timing. I had this exact same problem today, I opted for JSON.Net serialisation to JSON. Worked well for me but my classes weren't quite so complex. – kidshaw Jun 17 '15 at 21:25
  • Check this answer: http://stackoverflow.com/questions/16864447/tool-to-write-an-object-instance-to-c-sharp-for-unit-testing/16865470#16865470 – Thomas C. G. de Vilhena Jun 17 '15 at 21:28
  • If the large class is more than just a property bucket and does stuff including interacting with other classes (I'm guessing not via interfaces), I don't think writing unit-tests for that one class is going to help you much. Writing some integration tests that ensure the *systems* behaviour, rather than the individual class is likely to give you more confidence in the system when you start moving the functionality about. http://stackoverflow.com/q/30614653/592182 – forsvarir Jun 17 '15 at 22:12
  • Thanks @forsvarir never used shims before but this looks exactly what I need. – jeffpainter Jun 17 '15 at 23:27
  • @jeffpainter I try and constantly improve the [Object Exporter](https://github.com/OmarElabd/ObjectExporter) plugin so if their is a specific area that it seems to be insufficient in, please do let me know. – Omar Elabd Jul 22 '15 at 20:19
  • 1
    Thanks, Omar! I don't think it's due to any insufficiency in your plugin, it's just that the class I was trying to Serialize is enormous, self-referential, tightly-coupled to lots of other enormous classes, and it contains DependencyObjects (which are apparently difficult or maybe even impossible to serialize). I love the Object Exporter, and it's a very useful tool to have in the toolbox, just maybe the wrong tool for the job I was trying to use it for. – jeffpainter Jul 23 '15 at 21:27

0 Answers0