evanescent
evanescent

Reputation: 1373

How to test that an object will be deallocated after teardown

Is there a way to test that my object will be deallocated after calling teardown on the object in a XCTest. I'm thinking of doing something like this:

[self.client teardown];
__weak typeof(self.client) weakClient = self.client;
XCTestExpectation *teardownExpectation = ...;
dipatch_time_t delay = ...
dispatch_after(delay, dispatch_get_main_queue(), ^{
    XCTAssertNil(weakClient);
    [teardownExpectation fulfill];
}
self.client = nil;
[self waitForExpectationsWithTimeout:5.0 ...];

But the problem with this is that the autorelease pool would not drain so my object would never actually be deallocated.

Is there any other way to test this?

Upvotes: 2

Views: 506

Answers (1)

cotugs
cotugs

Reputation: 234

You could put it in your own autorelease pool and then drain it, to simulate the conditions of what would happen at the end of a run loop. Obviously here you're keeping the run loop going by waiting for the expectations on your test. But the better way to test allocations is to use Instruments and the leaks/allocations tool.

Upvotes: 1

Related Questions