Right now I work a lot with code generation and reflectivity and often I need to change source code of a class to test some behavior.
But if I change it, then I also need to change it back, which is annoying. So how to go about it?
Can you see any problem with this? If one of the tests fails then I lose the code.
I could use
#tearDown instead and have some mechanism around.
Although extra work is needed around, in the test method itself I need just a single extra line, which I like.
Alternatively I could just create an anonymous subclass, do whatever I want with it and then not care about it.
primitiveChangeClassTo: for? To change the class of an object without reinitialization or anything.
Be warned though, you can’t just warp between random classes as they have different attributes and layout. But anonymous subclasses, and parent classes should generally be fine.
Which approach is the best? Certainly not the first one.
I prefer the last one the most as I don’t really need to worry about fixing things — I’ve never broken anything to begin with! But if I don’t control the instances of the original class (i.e. I can’t change their class), then I have no choice but to use the second one.