Search code examples
iosswiftunit-testingmagicalrecord

MagicalRecord completion block is not called under test target


MagicalRecord.saveWithBlock({ context in

        if let items = dictionary["items"] as? Array<NSDictionary> {
            for itemInfo in items {
                DBItem.findOrUpdateItemWithDictionary(itemInfo, inContext: context)
            }
        }
        //is called
        if let sets = dictionary["item_sets"] as? Array<NSDictionary> {
            for setInfo in sets {
                DBSet.findOrUpdateSetWithDictionary(setInfo, inContext: context)
            }
        }

        }, completion: { finished, error in

        completionBlock(error) //is not called
})

This is how I setup my core data stack:

MagicalRecord.setupCoreDataStackWithInMemoryStore()

Solution

  • The saveWithBlock: method is executed asynchronously. The test might be finished before calling completion block though I do not know how is your test code.

    https://github.com/magicalpanda/MagicalRecord/blob/c7b14f658b4fca32f8d33f8f76160203053ce4b9/MagicalRecord/Core/MagicalRecord%2BActions.m#L14-L35

    Could you try to change the saveWithBlock: method to saveWithBlockAndWait:? It is executed syncronously. Or wait to execute asynchronous call with XCTestExpectation?