unit-testing – 如何在Swift中对NSFetchedResultsController进行单元测试

前端之家收集整理的这篇文章主要介绍了unit-testing – 如何在Swift中对NSFetchedResultsController进行单元测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Swift应用程序,它使用NSFetchedResultsController从持久存储中获取List对象:
let fetchedResultsController: NSFetchedResultsController = ...
var error : NSError?
fetchedResultsController.performFetch(&error)
if let error = error {
    NSLog("Error: \(error)")
}
let lists: [List] = fetchedResultsController.fetchedObjects! as [List]
NSLog("lists count = \(lists.count)")
for list: List in lists {
    NSLog("List: \(list.description)")
}

它的工作方式与预期的一样,我将List对象描述打印到控制台.
我想为我的应用程序编写一些单元测试,所以我创建了扩展XCTestCase的类.代码编译没有问题,测试运行,但不幸的是我无法在该上下文中获取List对象.

我在控制台中获得的所有内容都是List对象的数量和致命错误

lists count = 59
fatal error: NSArray element Failed to match the Swift Array Element type

线路上升:

for list: List in lists {

我很确定我已经正确配置了目标,因为我可以创建List对象并将其插入到托管对象上下文中,而不会出现我的应用程序源代码以及单元测试源代码的问题.我遇到的唯一问题是从测试单元获取.我想知道为什么在模拟器中运行应用程序时提取工作正常,并且在单元测试期间执行时失败.

任何可能出错的想法都将受到赞赏.

更新:

更具体地说,我的实现如何,这里是我正在玩的完整代码示例:

var error: NSError? = nil

let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask)
let applicationDocumentsDirectory = urls[urls.count-1] as NSURL

let modelURL = NSBundle.mainBundle().URLForResource("CheckLists",withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)

var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
let url = applicationDocumentsDirectory.URLByAppendingPathComponent("CheckLists.sqlite")
if coordinator!.addPersistentStoreWithType(NSsqliteStoreType,configuration: nil,URL: url,options: nil,error: &error) == nil {
    NSLog("Error1: \(error)")
    abort()
}

var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator

let fetchRequest = NSFetchRequest()
fetchRequest.entity = NSEntityDescription.entityForName("List",inManagedObjectContext: managedObjectContext)
fetchRequest.sortDescriptors = [ NSSortDescriptor(key: "name",ascending: true) ]

let fetchedResultsController = NSFetchedResultsController(
    fetchRequest: fetchRequest,managedObjectContext: managedObjectContext,sectionNameKeyPath: nil,cacheName: "ListFetchedResultsControllerCache"
)

fetchedResultsController.performFetch(&error)
if let error = error {
    NSLog("Error2: \(error)")
    abort()
}

let fetchedObjects: [AnyObject]? = fetchedResultsController.fetchedObjects
if let fetchedObjects = fetchedObjects {
    NSLog("Fetched objects count: \(fetchedObjects.count)")
    for fetchedObject in fetchedObjects {
        NSLog("Fetched object: \(fetchedObject.description)")
    }
}
else {
    NSLog("Fetched objects array is nil")
}

let fetchedLists: [List]? = fetchedResultsController.fetchedObjects as? [List]
if let fetchedLists = fetchedLists {
    NSLog("Fetched lists count: \(fetchedLists.count)")
    for fetchedList in fetchedLists {
        NSLog("Fetched list: \(fetchedList.description)")
    }
}
else {
    NSLog("Fetched lists array is nil")
}

当我从应用程序的源代码执行它,在模拟器中运行应用程序时,控制台输出如下所示:

Fetched objects count: 3
Fetched object: <CheckLists.List: 0x7a6866f0> (entity: List; id: 0x7a686020 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p2> ; data: {
    name = "List 1";
})
Fetched object: <CheckLists.List: 0x7a686930> (entity: List; id: 0x7a686030 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p1> ; data: {
    name = "List 2";
})
Fetched object: <CheckLists.List: 0x7a686970> (entity: List; id: 0x7a686040 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p3> ; data: {
    name = "List 3";
})
Fetched lists count: 3
Fetched list: <CheckLists.List: 0x7a6866f0> (entity: List; id: 0x7a686020 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p2> ; data: {
    name = "List 1";
})
Fetched list: <CheckLists.List: 0x7a686930> (entity: List; id: 0x7a686030 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p1> ; data: {
    name = "List 2";
})
Fetched list: <CheckLists.List: 0x7a686970> (entity: List; id: 0x7a686040 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p3> ; data: {
    name = "List 3";
})

但是,当我从单元测试中执行此代码时,我得到此输出

Fetched objects count: 3
Fetched object: <CheckLists.List: 0x7a07df50> (entity: List; id: 0x7a07d7e0 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p2> ; data: {
    name = "List 1";
})
Fetched object: <CheckLists.List: 0x7a07e190> (entity: List; id: 0x7a07d7f0 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p1> ; data: {
    name = "List 2";
})
Fetched object: <CheckLists.List: 0x7a07e1d0> (entity: List; id: 0x7a07d800 <x-coredata://7A87B5BE-C2FA-4150-B9E3-879FDE07F0B9/List/p3> ; data: {
    name = "List 3";
})
Fetched lists array is nil

我希望它更容易理解问题所在.不知何故,这句话:

let fetchedLists: [List]? = fetchedResultsController.fetchedObjects as? [List]

当app在模拟器中运行时,会生成一个List对象数组,但是当从单元测试执行时,它无法生成nil.

该问题与目标配置相关联.我通过一些解决方法解决了这个问题.

以前,为了使List实体类可以在我的单元测试目标中访问,我已将它添加到此目标.因此,List类有两个目标.实际上,Swift已知有两个List类,每个目标对应一个:MyAppTarget.List和MyUnitTestTarget.List. Retched结果控制器返回MyAppTarget.List对象的数组,但在单元测试目标中,List被假定为MyUnitTestTarget.List类.这就是这行代码

let fetchedLists: [List]? = fetchedResultsController.fetchedObjects as? [List]

从单元测试目标执行时生成nil,而不是从主目标执行时生成的正确数组.为了解决这个问题,我将其更改为:

let fetchedLists: [MyAppTarget.List]? = fetchedResultsController.fetchedObjects as? [MyAppTarget.List]

并使List类公开.在那次改变之后,它会像预期的那样工作

但是,对我来说,MyAppTarget.List无法转换为MyUnitTestTarget.List有点让人困惑.而且,这意味着我需要公开每个实体NSManagedObject子类,以便在单元测试中使用它.到目前为止,我找不到更好的解决方案.

也许有更好的方法解决这个问题.我没有看到告诉NSFetchedResultsController它应该返回主目标中的MyAppTarget.List和单元测试目标中的MyUnitTestTarget.List的选项.它将始终使用.xcdatamodeld文件中给定实体的配置.此外,即使有一种方法将MyAppTarget.List转换为单元测试中的MyUnitTestTarget.List,它仍然需要List类是公共的.

更新:

我找到了一种在运行时更改NSFetchedResultsController返回的实体类的方法.这是一个更清晰简单的解决方案:https://stackoverflow.com/a/25858758/514181

它允许在单元测试中无缝地使用CoreData实体,而无需强制转换或使实体类公开.

原文链接:https://www.f2er.com/swift/320234.html

猜你在找的Swift相关文章