I’d like to write a logic test to test for the case when [super init] fails.
I have an init method like this:
- (id)init
{
if ((self = [super init]) != nil)
{
// init my members
}
return self;
}
Does anyone know how I can get [super init] to return nil to test this case? “super” is NSObject in this case and I’m currently using SenTestKit for logic tests.
You could, but it’d be very complex. You’d need to swizzle
initonNSObject(google “method swizzle”). There’s really no reason to do that. The only way thatNSObject initcan fail is if you’re completely out of memory, and that’s such an obscure case in desktop and mobile apps that the test for it isn’t worth the overhead of creating it. You wouldn’t even be able to write an ObjC error handler for it, because you’re completely out of memory and ObjC can barely function without allocating a little memory.If you really wanted to test this case, you’re better off using a test class that returns
nilfrominitand check that the rest of the system works. But this is, and should be, a really obscure case that I wouldn’t pursue.