不在测试目标下调用MagicalRecord完成块

时间:2015-08-07 07:37:10

标签: ios swift unit-testing magicalrecord

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
})

这就是我设置核心数据堆栈的方式:

MagicalRecord.setupCoreDataStackWithInMemoryStore()

1 个答案:

答案 0 :(得分:1)

saveWithBlock:方法是异步执行的。虽然我不知道你的测试代码是什么,但测试可能在调用完成块之前完成。

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

您可以尝试将saveWithBlock:方法更改为saveWithBlockAndWait:吗?它是同步执行的。或者等待与XCTestExpectation执行异步调用?