gonzalezreal / Groot

From JSON to Core Data and back.
Other
534 stars 61 forks source link

Could not locate the entity for myappTests.Example when inside XCTestCase #81

Closed sanmai closed 7 years ago

sanmai commented 7 years ago

How one is supposed to write tests for objects that use Groot?

For example, where I can create an entity inside an XCTestCase just fine:

let entity = NSEntityDescription.insertNewObject(
                                forEntityName: String(describing: Example.self), 
                                into: inMemoryManagedObjectContext)

Trying the same thing from XCTestCase:

let updateJSON: JSONDictionary = [
    "id": 1,
    "title": "Hello",
]

let item: Example? = try? object(fromJSONDictionary: updateJSON, inContext: inMemoryManagedObjectContext)

Fails with:

fatal error: Could not locate the entity for myappTests.Example.: file myapp/Pods/Groot/Groot/Groot.swift, line 56
sanmai commented 7 years ago

Also this wouldn't work:

let item = Example(context: inMemoryManagedObjectContext);

Failing with:

failed: caught "NSInvalidArgumentException", "An NSManagedObject of class 'myappTests.Example' must have a valid NSEntityDescription."

After exploring NSManagedObjectModel I came to realise that internally it uses myapp.Example no matter from where it is used. So if one uses exact full name for classes inside tests even having imported the app module, everything works:

let item: myapp.Example? = try? object(fromJSONDictionary: updateJSON, inContext: inMemoryManagedObjectContext)