Injecting OCMock objects

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Injecting OCMock objects

Postby jbjon » 17 May 2011, 13:52

I'm trying to inject a Mock object into a class but am then running into trouble when the class tries to run it's dealloc method.

Example:
If the class under test has init: and an initWithDependency:(Thingy *)
So if the class had an instance variable of myThingy:
Code: Select all
Thingy *myThingy;


Then the init in the implementation would normally do the following:
Code: Select all
- (id)init
{
  Thingy *newThingy = [[Thingy alloc] init];
  myThingy = newThingy;
  [newThingy release];
  return self;
}

and the initWithDependency would simply do:
Code: Select all
- initWithDependency:(Thingy *)newThingy
{
  myThingy = newThingy;
}


and the dealloc method then tries to release the instance variable
Code: Select all
- (void)dealloc
{
  [myThingy release];
}


This fails when I try and pass in an OCMockObject. As the mockForObject method is a class level method, there is no need to call 'release' on the mockObject as it's in the AutoRelease pool.

So given I'm trying to not amend my original class design too much to allow it to be mock unit-tested, is there any obvious way around this issue, where an instance variable could be set with an object created from an alloc, or a mock object that's being maintained by the autorelease pool?

Cheers,
Jonathan
jbjon
 
Posts: 2
Joined: 17 May 2011, 13:22

Re: Injecting OCMock objects

Postby jbjon » 18 May 2011, 09:56

I think I've fixed it now. Missed out some self selectors which were causing the problem.
Working fine now, thanks for listening :)
jbjon
 
Posts: 2
Joined: 17 May 2011, 13:22


Return to OCMock