Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

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

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):

In the application source file:

func foo() {
   assert(true)
}

In the UI tests:

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

I have foud similar issue reported here: https://forums.developer.apple.com/thread/20609 but no solution. Seems to me like the @testable simply doesn't work correctly. The guy on the developer.apple.com tried 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 @testable should just make everything work, and it doesn't look like it at the moment. Any help appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@testable import MainModule won'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:

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).

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...