How to validate basic data in Swift

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?
+7
ios swift core-data nsmanagedobjectcontext
source share
2 answers

I was about to point you to Swift, Core Data, and unit testing, but I see that you already found it. :)

This article does not say much about where your files should exist (i.e. which one is specified). You should not add subclasses of NSManagedObject (or any files in fact) to both goals. I found that this leads to all kinds of errors and cryptic errors being detected.

And definitely NOT . This is a terrible hack.

Instead, make your classes public and import MyAppTarget in your XCTestCase files. Even better, your model should be within its own framework, as I mention in a recent talk (the video will be published in a few weeks in the .io area). This makes the model namespace very understandable and generally simpler. Then you will need to import MyAppModel wherever you access the managed objects.

I also have a new framework, JSQCoreDataKit , which is designed to simplify the use of Core Data in Swift. One of the key components of this structure is CoreDataStack , which you can initialize with in-memory storage for your tests. There is a demo application with examples and well-commented unit tests.

+10
source share

I believe this was recently updated (iOS 9 / Swift 2.0) to have a test keyword on an imported target, meaning that the target inner classes (by default) are made public. From the docs:

enter image description here

So, to add the answer above to jessesquires, add @testable to your import, and this should solve the unit test errors:

 @testable import MyAppTarget 
+2
source share

All Articles