Swift不能在Xcode测试中测试核心数据? [英] Swift cannot test core data in Xcode tests?
问题描述
我正在使用一个使用核心数据的iOS项目。我使用swift。
核心数据堆栈设置正确,似乎都很好。
我为一个名为TestEntity的实体(NSManagedObject)创建了一个类。
类如下所示:
import UIKit
import CoreData
class TestEntity:NSManagedObject {
@NSManaged var name:NSString
@NSManaged var age:NSNumber
}
所以,我试着在代码中插入一个新的TestEntity使用这行代码:
let te:TestEntity = NSEntityDescription.insertNewObjectForEntityForName(TestEntity,inManagedObjectContext:ctx)as TestEntity
那么会出现以下错误:
我看到一些堆栈溢出的答案,说我需要担心模块名称。然后我查看了文档:
- 确保无的常规课程中有目标成员资格指向测试目标。只有具有单元测试代码的类才应设置为测试目标。
- 将
@testable
行添加到您的测试类:
import XCTest
@ testable import MyApp
class MyAppTests:XCTestCase {
}
如果您仍遇到问题,请尝试以下其他提示: https:// forums.developer.apple.com/message/28773#28949
我和这个人打了一段时间,所以我希望它能帮助别人。 / p>
I am working on a iOS project that uses core data. I am using swift. The Core Data stack is setup right and all seems to be fine. I have created a class for an entity (NSManagedObject) called TestEntity. The class looks like this:
import UIKit
import CoreData
class TestEntity: NSManagedObject {
@NSManaged var name: NSString
@NSManaged var age: NSNumber
}
So, then I try to insert a new TestEntity in code using this line of code:
let te: TestEntity = NSEntityDescription.insertNewObjectForEntityForName("TestEntity", inManagedObjectContext: ctx) as TestEntity
I then get this error:
I have seen some answers on stack overflow that say that I need to worry about the module name. So then I looked that up on the docs: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html
Then I went in the core data entity for TestEntity and in the class field I entered myAppName.TestEntity
When I run the app this line:
let te: TestEntity = NSEntityDescription.insertNewObjectForEntityForName("TestEntity", inManagedObjectContext: ctx) as TestEntity
still gives me the same error.
What else could I be doing wrong?
EDIT: So, I was able to make the app not crash anymore by changing the TestEntity NSManagedObject class to: import UIKit import CoreData
@objc(TestEntity) class TestEntity: NSManagedObject {
@NSManaged var name: NSString
@NSManaged var age: NSNumber
}
So, I added the @objc(TestEntity) in it. This works with or without adding the appName before the TestEntity class name in the core data data model inspector.
This works, but, when I run tests this line still crashes:
let te: TestEntity = NSEntityDescription.insertNewObjectForEntityForName("TestEntity", inManagedObjectContext: ctx) as TestEntity
So I found that this is an issue for other people: How to access Core Data generated Obj-C classes in test targets?
How can we get core data to work in tests in swift. I am NOT using a bridging header in the app target and it all works great. The test target still crashes though.
How can I fix the test target so it can run core data tests?
With Xcode 7, and @testable
, you should no longer need to update the managedObjectClassName
or use other hacks. Here's what I did to get it working in Xcode 7.2.
- Set your test target Host Application and check "Allow testing Host Applications APIs".
- Make sure none of your regular classes have a Target Membership pointing to the Test target. Only classes with unit test code should be set to the Test target.
- Add the
@testable
line to the top of all of your test classes:
import XCTest
@testable import MyApp
class MyAppTests: XCTestCase {
}
If you're still having issues you may want to try these additional tips: https://forums.developer.apple.com/message/28773#28949
I fought with this one for a while so I hope it helps someone else out.
这篇关于Swift不能在Xcode测试中测试核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!