iOS 单元测试:Class 在两者中都实现了

iOS Unit Testing: Class is implemented in both

我正在使用 Swift 2 和 Xcode 7 构建一个 iOS 9 应用程序。

我的应用程序 运行 在将它部署到我的 phone 时很好,但是当我 运行 进行任何单元测试时,我收到很多 类:

Class _TtC5<AppName>19<ClassName> is implemented in both /Users/<Username>/Library/Developer/CoreSimulator/Devices/<UUID>/data/Containers/Bundle/Application/<UUID</<AppName>.app/<AppName> and /Users/<Username>/<Path/To/Workspace>/DerivedData/<AppName>/Build/Products/Debug-iphonesimulator/<AppName>.xctest/<AppName>. One of the two will be used. Which one is undefined.

有什么想法吗?

仅将测试 class 添加到您的测试目标和 iPhone 应用 class 的 none 中。然后只需将您的应用程序名称作为模块导入到您的测试 class 之上,即可访问所有应用程序 classes.

@testable import MyAppName

@Mundi 的回答在某些情况下是正确的。

但是,我遇到了同样的错误,导致它的原因是 Cocoa Pods 的 libPods.a 文件在应用程序目标和测试目标中被链接。

重复 class 实现,而 运行 测试套件:在我的例子中,因为测试目标有一个 主机应用程序 ,你不需要link third-party 框架。我必须从构建设置中清除 "Other Linker Flags" (OTHER_LDFLAGS),因为它 linking 到工作区的所有 CocoaPods。

我在 XCTest 中遇到了同样的错误。记得selectAllow testing Host Application APIs.

然后在 Build Phases 中为您的 XCTest Target - 就像其他人所说的 - 确保你在 Compile Sources 中有零个文件,这些文件已经在 Host Application 中编译。