views:

617

answers:

1

Hi, I'd like to test some code with OCMock.

The innards of the code are calling [NSObject isKindOfClass] on the mock object I'm providing to the code, like so:

if ([object isKindOfClass:[FancyClass class]]) { ...}

However, when I provide an OCMockObject-based mock created like this:

mock = [OCMockObject mockForClass:[FancyClass class]];

it appears that it does not pass the isKindOfClass test.

Any suggestions?

+2  A: 

The general rule is that if you're calling isKindOfClass: and you're not passing one of the plist classes (e.g., NSString or NSNumber), You're Doing It Wrong.

If the method does two or more different things depending on which class its argument is, rend it into multiple methods, and test each method separately.

If the method does only one thing, but has to interact with the object differently depending on which class it is, then:

  1. Make a protocol. (Protocols in Obj-C are called “interfaces” in some other OO languages, such as Java.)
  2. Make the many classes all conform to the protocol. If necessary, use categories to add the necessary methods from outside.
  3. Make the method under test check for conformance to the protocol, in place of the current isKindOfClass: check.
  4. Make the method under test use the methods in the protocol.
Peter Hosey
The list of suggestions seem good, but I would have to disagree that using isKindofClass should only ever take foundation classes is not right.For instance, it's of great use when going through the array loaded via nib and looking for the UITableViewCell you created...
Kendall Helmstetter Gelner
Wouldn't it be better to test for what you need from the cell, rather than where it resides in the class hierarchy? (I don't know Cocoa Touch, so I can't get more specific.)
Peter Hosey