Search code examples
swiftunit-testingrestkitcocoapodsnsmanagedobjectcontext

managed object context nil restkit while unitTesting


I am developing unit tests for my application using restkit (0.24.1) integrated using cocoapods:

pod "RestKit/Testing", "~> 0.24.1"
pod "RestKit", "~> 0.24.1"

I am getting a error message saying that the managedObjectContext is nil. I beg to differ because I already have lots of unit tests using the same object context and they all seem to work... :/

Restkit setup:

lazy var persistentStoreCoordinator: RKManagedObjectStore? = {
    var coordinator = RKManagedObjectStore(managedObjectModel: self.managedObjectModel)
    self.objectManager!.managedObjectStore = coordinator

    coordinator.createPersistentStoreCoordinator()

    var storePath: NSString = RKApplicationDataDirectory().stringByAppendingPathComponent(self.storeFilename)

    var e: NSError?
    coordinator.addSQLitePersistentStoreAtPath(storePath as String, fromSeedDatabaseAtPath: nil, withConfiguration: nil,
        options: [
            NSInferMappingModelAutomaticallyOption: true,
            NSMigratePersistentStoresAutomaticallyOption: true
        ], error: &e)

    if(e != nil){
            var error: NSError? = nil
            coordinator = nil
            // Report any error we got.
            let dict = NSMutableDictionary()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = "There was an error creating or loading the application's saved data."
            dict[NSUnderlyingErrorKey] = error
            error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict as [NSObject : AnyObject])
            Logger.Error("Serious error \(error), \(error!.userInfo)")
            abort()
    }

    coordinator.createManagedObjectContexts()
    coordinator.managedObjectCache = RKInMemoryManagedObjectCache(managedObjectContext: coordinator.persistentStoreManagedObjectContext)

    return coordinator
    }()

The unit test:

class TestRestMappers: XCTestCase {

    let rootEndpoint = RootEndpoint()

    override func setUp() {
        super.setUp()

        let testTargetBundle = NSBundle(identifier: "anita-borg.malaria-iosTests")
        RKTestFixture.setFixtureBundle(testTargetBundle)
    }

    func testRootEndpointMapper() {
        let parsedJson: AnyObject? = RKTestFixture.parsedObjectWithContentsOfFixture("api.json")

        let mapping = rootEndpoint.mapping

        let test = RKMappingTest(forMapping: mapping, sourceObject: parsedJson, destinationObject: nil)

        test.addExpectation(RKPropertyMappingTestExpectation(sourceKeyPath: "users", destinationKeyPath: "users"))

        XCTAssertTrue(test.evaluate)
    }
}

The error message I get from the console is:

*** Assertion failure in -[RKMappingTest dataSourceForMappingOperation:], (...): error: 
-[malaria_iosTests.TestRestMappers testRootEndpointMapper] : XCTAssertTrue failed: throwing "Cannot test an `RKEntityMapping` with a nil managed object context." - 

Test Case '-[malaria_iosTests.TestRestMappers testRootEndpointMapper]' failed (0.010 seconds).

A snippet of the mapping where I connect the store:

let managedObjectStore: RKManagedObjectStore = CoreDataStore.sharedInstance.persistentStoreCoordinator!
let rootMap = RKEntityMapping(forEntityForName: name, inManagedObjectStore: managedObjectStore)

I already searched in many places the source of the problem and I can't seem to find a solution.

Thank you in advance

edit:

NSManagedObjectContext:

lazy var backgroundContext: NSManagedObjectContext? = {
    let coordinator = self.store.persistentStoreCoordinator?.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }

    var backgroundContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
    backgroundContext.persistentStoreCoordinator = coordinator
    return backgroundContext
    }()

Solution

  • Tests require configuration. Generally you should assume that if you don't explicitly give a test some information then it doesn't have any information. So, you need to give the test a context to work with (and potentially a data source in your specific case).

    test.managedObjectContext = ...