Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing Model Classes that inherit from NSManagedObject
    text
    copied!<p>So...I'm trying to get unit tests set up in my iPhone App but I'm having some issues. I'm trying to test my model classes but they inherit directly from NSManagedObject. I'm sure this is a problem but I don't know how to get around it. </p> <p>Everything is building and running as expected but I get this error when calling any method on the class I'm testing: </p> <p><code>Unknown.m:0:0 unrecognized selector sent to instance 0xc2b120</code></p> <p>If I follow <a href="http://chanson.livejournal.com/115621.html" rel="noreferrer">this structure</a> to create my object in my tests I end up with another error entirely but it still doesn't help me.</p> <p>If I instantiate my model like this:</p> <pre><code>entry = [[TimeEntry alloc] initWithEntity:nil insertIntoManagedObjectContext:nil]; </code></pre> <p>Then I end up with this error at runtime:</p> <p><code>An NSManagedObject of class 'TimeEntry' must have a valid NSEntityDescription.</code></p> <p>If I try it like this:</p> <pre><code>entry = [[TimeEntry alloc] init]; </code></pre> <p>Then I end up with this error:</p> <p><code>unrecognized selector sent to instance 0xc2b120</code></p> <p>And if I follow the pattern <a href="http://iamleeg.blogspot.com/2010/01/unit-testing-core-data-driven-apps-fit.html" rel="noreferrer">laid out here</a>:</p> <pre><code>model = [[NSManagedObjectModel mergedModelFromBundles: nil] retain]; NSLog(@"model: %@", model); coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model]; store = [coord addPersistentStoreWithType: NSInMemoryStoreType configuration: nil URL: nil options: nil error: NULL]; ctx = [[NSManagedObjectContext alloc] init]; [ctx setPersistentStoreCoordinator: coord]; entry = (TimeEntry *)[NSEntityDescription insertNewObjectForEntityForName:@"TimeEntry" inManagedObjectContext:ctx]; </code></pre> <p>Then I get this error:</p> <p><code>could not locate an entity named 'TimeEntry' in this model.</code></p> <p>Basically my question is this: how can I test a class that inherits from NSManagedObject?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload