I am stock with the following problem: I am programming an app in swift. I have a class function (mapCategories) of a managed object (called Category) that I want to Unit test (XCTest).
myCategoryFunction gets an NSDictionary and maps its content in to a list of Categories [Category] and returns it. to do the mapping i had to create Category objects using the following code:
class func mapCategories(myDictionary: NSDictionary!) -> [Category]{
var categories: [Category] = []
/*
... some code here.
*/
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Category", inManagedObjectContext: managedObjectContext!)
var category = Category(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
/*
... some code here.
*/
return Categories
}
when i run the application, the code runs fine and works as intended but when i run the unit test of this function it crashes.
1st i get the following error:
which occurs in the following line:
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
and when i continue the program execution, it crashes when getting the managedObjectContext as follows:
I tried to find a solution or a workaround and tried the following:
but it didn't work. Does anyone have a solution that works?
thanks in advance,