如何在Swift中正确测试核心数据 [英] How to test Core Data properly in Swift

查看:124
本文介绍了如何在Swift中正确测试核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经有很多主题,但我还没有找到一个可行的解决方案Swift(Xcode 6.2)。

There are quite a few subjects on this already, but I have yet to find a solution that is workable for Swift (Xcode 6.2).

要在Swift中测试Core Data支持的类,我会生成新的托管对象上下文,然后注入到我的类中。

To test Core Data backed classes in Swift, I generate new Managed Object Contexts that I then inject into my classes.

//Given   
let testManagedObjectContext = CoreDataTestComposer.setUpInMemoryManagedObjectContext()
let testItems = createFixtureData(testManagedObjectContext) as [TestItem]
self.itemDateCoordinator.managedObjectContext = testManagedObjectContext

//When
let data = self.itemDateCoordinator.do()

//Then
XCTAssert(data.exists)

问题来自传递在测试到正在做的类。因为实体类是命名空间,Core Data不会获取适当的ManagedObject子类,而是递回一个 NSManagedObject 集合。当使用这些对象循环或做任何事情(在你的类中将是一个测试项目数组( [TestItem] )。

The issue comes from passing a MOC created in the Test to the class that's doing. Because entity classes are namespaced, Core Data won't fetch your the appropriate ManagedObject subclass and instead hands back a NSManagedObject set. When looping or doing anything with these objects (which in your class would be an array of test items ([TestItem]).

例如,冒犯的类 ItemDateCoordinator 会执行这个循环(从 NSFetchRequest

For example, the offending class ItemDateCoordinator would execute this loop (after pulling the relevant data from a NSFetchRequest)"

for testItem in testItems {
    testItem.doPart(numberOfDays: 10)
}

会导致:


致命错误:NSArray元素无法匹配Swift数组元素类型

fatal error: NSArray element failed to match the Swift Array Element type

此外,我遇到了一组信息,很多固定的答案:

Also, I have come across a collection of information without much of a solid answer:


  • 要在创建实体时投射实体,我一直使用 Jesse ,但这不适用于更大的测试范围。

  • 解决方案已发布在另一个问题上,该问题涉及在运行时切换类,但这不适用于我用实体继承。

  • 在这种情况下,是否有其他方法使用Core Data测试您的对象?你怎么做的?

  • To cast entities when creating them, I have been using a solution by Jesse, but that doesn't work on a larger scope of testing.
  • A solution has been posted on another question that involved swapping out the classes at runtime, but that hasn't worked for me with entity inheritance.
  • Is there another method to testing your objects with Core Data in this case? How do you do it?

推荐答案

我打算指向 Swift,Core Data和unit测试,但看到您已经找到它。 :)

I was about to point you toward Swift, Core Data, and unit testing but see you've already found it. :)

这篇文章没有详细说明你的文件应该存在于哪里(即,在哪个目标)。您不应 NSManagedObject 子类(或真正的任何文件)添加到两个目标。

That post doesn't elaborate much on where your files should exist (i.e., in which Target). You should not add NSManagedObject subclasses (or any files really) to both targets. I've found that this leads to all kinds hard discover bugs and cryptic errors.

并且绝对不要执行。这是一个可怕的黑客。

And definitely DO NOT do this. That is a terrible hack.

相反,让你的类公开和 import MyAppTarget 在你的 XCTestCase 文件。更好的是,您的模型应该在自己的框架中,正如我在最近的讲话(a视频将在几个星期内在realm.io上发布)。这样做使你的模型命名空间非常清楚,通常更容易处理。然后你需要 import MyAppModel 到你访问你的托管对象。

Instead, make your classes public and import MyAppTarget in your XCTestCase files. Better yet, your model should be in its own framework as I mention in my recent talk (a video will be posted in a few weeks on realm.io). Doing this makes your models namespace very clear and generally easier to deal with. Then you'll need to import MyAppModel everywhere you access your managed objects.

我也有一个新框架, JSQCoreDataKit ,旨在使Core Data在Swift中更易于使用。此框架的一个关键部分是 CoreDataStack ,您可以使用内存存储为您的测试初始化​​。有示例的示例应用程序,以及良好评论的单元测试。

I also have a new framework, JSQCoreDataKit that intends to make Core Data easier to use in Swift. One key part of this framework is the CoreDataStack which you can initialize using an in-memory store for your tests. There's demo app with examples, and well-commented unit tests.

这篇关于如何在Swift中正确测试核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆