I’m pretty new to TDD and unit testing and I’m giving it a try in a side project.
And I’m kinda stuck in unit testing the dealloc method.
We all know it is a good practice to release custom properties and set them to nil in the dealloc :
-(void) dealloc{
[myProperty release];
myProperty = nil;
[super dealloc];
}
How could I write a unit test that check this behaviour ?
Obviously, this does not work :
[myObject release]
STAssertNil(myObject.myProperty,@"myProperty should be released and set to nil in dealloc")
Any suggestion ?
thanks !
Assuming you could do the above, what would you actually be testing? Here is what i think:
testing that -release decrements the retainCount
testing that when retainCount reaches zero NSObject’s Garbage Collection mechanism calls -dealloc
testing that when -dealloc is called, myProperty is released
This is the wrong granularity for a unit test, points one and two are not your business to test – assume it is unit tested by apple. So, the only bit of the above that is a unit test is:
Now, this piece of code (quite rightly) has no logic whatsoever and produces no output. So, i believe that unit testing it is actually paranoid and unnessesary (akin to testing that the compiler actually works).
But if i did want to test it i would do it like so:
Obviously you should never normally directly call -dealloc, but if you wanted to unit-test -dealloc that is exactly what you should do (or it isn’t a unit-test). Given that this is extremely bad practise and the test gains you very little, i don’t think you should bother.