There are already a lot of stories on this, but I still have to find a solution that can be used for Swift (Xcode 6.2).
To test the classes supported by Core Data in Swift, I create new managed objects that are then injected into my classes.
//Given let testManagedObjectContext = CoreDataTestComposer.setUpInMemoryManagedObjectContext() let testItems = createFixtureData(testManagedObjectContext) as [TestItem] self.itemDateCoordinator.managedObjectContext = testManagedObjectContext //When let data = self.itemDateCoordinator.do() //Then XCTAssert(data.exists)
The problem is with passing the MOC created in the test to the class that does. Because object classes have a namespace, Core Data will not receive your corresponding subclass of ManagedObject and will instead return a set of NSManagedObject . When you cyclize or do something with these objects (which in your class will be an array of test elements ( [TestItem] ).
For example, the intruder class ItemDateCoordinator would execute this loop (after pulling the appropriate data from NSFetchRequest ) "
for testItem in testItems { testItem.doPart(numberOfDays: 10) }
will result in:
fatal error: NSArray element does not match Swift Array element type
In addition, I came across a collection of information without a significant answer:
- To create objects when they were created, I used the Jesse solution, but this does not work on a larger scope of testing.
- The solution was posted on another issue in which classes were involved at runtime, but this did not work for me with object inheritance.
- Is there any other way to test your objects with Core Data in this case? How do you do this?
ios swift core-data nsmanagedobjectcontext
Dandy
source share