【发布时间】:2015-03-28 04:19:01
【问题描述】:
这将是使用 swift 进行 CoreData 单元测试的常见问题。由于 swift 在普通模块和测试模块之间的命名空间差异,会发生 EXC_BREAKPOINT 异常。即使引入了一些解决方案,我仍在努力解决这个问题。
我做了什么,我的问题就在这里。
- 使用带有核心数据的单一应用程序模板使用检查选项创建新项目。
- 运行单元测试。没有发生错误。
- 将 CoreDataSampleTest 目标添加到 Appdelegate.swift。
- 将 CoreDataSampleTests.swift 中的 testExample() 更改如下。
func testExample() { let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate }
- 运行单元测试。发生了 EXC_BREAKPOINT。
- 在 AppDelegate.swift 中更改 managedObjectModel() 如下Swift cannot test core data in Xcode tests?。
lazy var managedObjectModel: NSManagedObjectModel = { // The managed object model for the application. This property is not optional... let modelURL = NSBundle.mainBundle().URLForResource("CoreDataSample", withExtension: "momd")! let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)! // Check if we are running as test or not let environment = NSProcessInfo.processInfo().environment as [String : AnyObject] let isTest = (environment["XCInjectBundle"] as? String)?.pathExtension == "xctest" // Create the module name let moduleName = (isTest) ? "CoreDataSampleTests" : "CoreDataSample" // Create a new managed object model with updated entity class names var newEntities = [] as [NSEntityDescription] for (_, entity) in enumerate(managedObjectModel.entities) { let newEntity = entity.copy() as NSEntityDescription newEntity.managedObjectClassName = "\(moduleName).\(entity.name)" newEntities.append(newEntity) } let newManagedObjectModel = NSManagedObjectModel() newManagedObjectModel.entities = newEntities return newManagedObjectModel }()
- 运行单元测试。 EXC_BREAKPOINT 再次发生。
我想知道两件事。
如何避免 EXC_BREAKPOINT 异常。测试方法似乎正常工作,但 EXC_BREAKPOINT 异常会在每个测试方法处临时停止进程。我必须每次都恢复它。运行测试方法非常困难。
如果我无法避免 EXC_BREAKPOINT,我想在执行单元测试时忽略 EXC_BREAKPOINT 异常。
任何帮助或建议都会对我有所帮助。
谢谢,
仅供参考:
Swift cannot test core data in Xcode tests?.
编辑:
XCode 版本为 6.2
【问题讨论】:
-
我建议您在创建新应用程序时使用检查选项更好地创建具有核心数据的新应用程序。试一次!!!
-
@NRV 感谢您的回复。我没有提到它,但我使用检查选项创建了带有核心数据的项目。我编辑了我的问题。