isKindOfClass returning different values for Test Cases

352 Views Asked by At

I have a method someMethod. This method, at some point has the following if-else condition.

- (void) someMethod {
    // ... some more code ... 
    if ([userArray[0] isKindOfClass:[Me class]]) {
        // some code
    }
    else {
        // some other code
    }
}

Now this if-condition is always met when I execute the code normally. But when I call it from one of my test-cases, the else-part gets executed instead. I am calling this method exactly the same way (it has no side-effects, etc).

When I debugged the thing in both normal run, and testing run. I saw something different.

While running in Test, the userArray had 1 object, (Me_Me_2 *)0x00007fa61d39dbf0. And while running it normally, the userArray had the same object, but there was one difference. It said (Me_Me_ *)0x00007fce71459ae0.

When I print the value of NSStringFromClass([userArray[0] class]), they both print "Me".

"Me" is a NSManagedObject.

Another interesting thing is, if I add an expression in the debugger and evaluate it, it always evaluates to true - ([((NSObject*)userArray[0]) isKindOfClass:[Me class]]) returns (bool)true. This is totally bizarre! If the condition is true, why does it ever go into the else block?

Now some questions -

  1. What is happening over here? Are Core Data objects treated different when running in tests?

  2. Why is the type of the object "Me_Me_2" while testing and "Me_Me_" otherwise? Why is it not just "Me"?

1

There are 1 best solutions below

0
On

This sounds similar to the following issue: isKindOfClass doesn't work as expected

In short, is the class being compared a target member of the test target? It should only be a target member of the application.