在 Xcode 7.1 的 UI 测试中访问应用程序模块时出现链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33755019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:12:57  来源:igfitidea点击:

Linker error when accessing application module in UI tests in Xcode 7.1

iosxcodelinkerxcode-ui-testingxcode7.1

提问by lawicko

I'm trying to implement some UI tests in my project. Everything goes fine as long as I keep it simple: record the test case, add some asserts, then run the test. This works fine, however when I try to access the application module from inside my test, the linker throws an error (See below):

我正在尝试在我的项目中实施一些 UI 测试。只要我保持简单,一切都会顺利:记录测试用例,添加一些断言,然后运行测试。这工作正常,但是当我尝试从测试内部访问应用程序模块时,链接器会引发错误(见下文):

In the application source file:

在应用程序源文件中:

func foo() {
   assert(true)
}

In the UI tests:

在 UI 测试中:

import XCTest
@testable import MyApp

func testExample() {
    foo()
}

Error:

错误:

Undefined symbols for architecture i386: "MyApp.foo () -> ()", referenced from: MyAppUITests.MyAppUITests.testExample (MyAppUITests.MyAppUITests)() -> () in MyAppUITests.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Undefined symbols for architecture x86_64: "MyApp.foo () -> ()", referenced from: MyAppUITests.MyAppUITests.testExample (MyAppUITests.MyAppUITests)() -> () in MyAppUITests.o ld: symbol(s) not found for architecture x86_64

架构 i386 的未定义符号:“MyApp.foo () -> ()”,引用自:MyAppUITests.MyAppUITests.testExample (MyAppUITests.MyAppUITests)() -> () in MyAppUITests.o ld: symbol(s) not found for体系结构 i386 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

架构 x86_64 的未定义符号:“MyApp.foo () -> ()”,引用自:MyAppUITests.MyAppUITests.testExample (MyAppUITests.MyAppUITests)() -> () in MyAppUITests.o ld: symbol(s) not found for架构 x86_64

I have foud similar issue reported here: https://forums.developer.apple.com/thread/20609but no solution. Seems to me like the @testablesimply doesn't work correctly. The guy on the developer.apple.comtried to workaround by adding the Test Host and Bundle Loader in the settings, but I don't think this is the correct approach. I think the @testableshould just make everything work, and it doesn't look like it at the moment. Any help appreciated!

我在这里报告了类似的问题:https: //forums.developer.apple.com/thread/20609但没有解决方案。在我看来,它@testable根本无法正常工作。developer.apple.com上的那个人试图通过在设置中添加测试主机和捆绑加载器来解决,但我认为这不是正确的方法。我认为@testable应该只是让一切正常,现在看起来不像。任何帮助表示赞赏!

回答by Jing Li

@testable import MainModulewon't work for UI test, though it would enable code completion (may make you feel it works). It's designed only for unit test so far. And it would lead to build failure, something like:

@testable import MainModule不适用于 UI 测试,但它会启用代码完成(可能会让您觉得它有效)。到目前为止,它仅用于单元测试。它会导致构建失败,例如:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The workaround is to add the source code file to UI test target as well, then it will work out of the box (even without @testable import).

解决方法是将源代码文件也添加到 UI 测试目标,然后它会开箱即用(即使没有@testable import)。

File Inspector-> Target Membership-> check UI test target(in addition to main target)

File Inspector-> Target Membership-> 检查UI 测试目标(除了主要目标)

Hope Apple will fix it soon so that we can have a cleaner way to use it.

希望 Apple 尽快修复它,以便我们可以更干净地使用它。

回答by Sushant

The UI tests are a separate module from the app, therefore not run inside your app as a logic test would. The only way for them to share code is to compile in all the app files you need to share between the two modules. Check this blog for how you can achieve that, https://www.bignerdranch.com/blog/ui-testing-in-xcode-7-part-1-ui-testing-gotchas/

UI 测试是独立于应用程序的模块,因此不会像逻辑测试那样在您的应用程序内运行。他们共享代码的唯一方法是编译您需要在两个模块之间共享的所有应用程序文件。查看此博客以了解如何实现这一目标,https://www.bignerdranch.com/blog/ui-testing-in-xcode-7-part-1-ui-testing-gotchas/

Also found a radar filed here, https://openradar.appspot.com/23116258

还在这里找到了一个雷达,https://openradar.appspot.com/23116258

回答by ScottyBlades

@testable import <yourApp>could actually be causing this bug. Just take out the line. It isn't needed for UI Tests.

@testable import <yourApp>实际上可能导致此错误。把线拿出来就行了。UI 测试不需要它。

"UI tests run outside your app in a separate process. You can't access app code from inside a UI test - that's intentionally part of the design. Use unit testing for tests that need to access the app code and UI tests to automate user interaction testing"

“UI 测试在您的应用程序外部在单独的进程中运行。您无法从 UI 测试内部访问应用程序代码 - 这是设计的一部分。使用单元测试进行需要访问应用程序代码的测试和 UI 测试以自动化用户交互测试”

Notice at 6 minutes in, @testable isn't used by Apple Developers. https://www.youtube.com/watch?v=7zMGf-0OnoU&t=1316s

请注意,在 6 分钟后,Apple Developers 未使用 @testable。https://www.youtube.com/watch?v=7zMGf-0OnoU&t=1316s