Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run tests in a Xcode project that imports another framework

I'm trying to write an open source app to show how you can write client + server code in Swift. The source code is located here: https://github.com/haaakon/QuizWorld (QW for short)

The app is using a framework, located here: https://github.com/haaakon/QuizWorld-API (QWAPI) to access the API. I've imported the QWAAPI as a project into the QW app, it runs fine in simulator, but when running the tests, it doesn't compile because of this Error:

Undefined symbols for architecture x86_64:
"QuizWorld.QuestionViewModel.__allocating_init () -> QuizWorld.QuestionViewModel", referenced from

This is from only one line of code in the test:

let a = QuestionViewModel()

This means that the test target does not correctly get the imported module in. The imports are:

@testable import QuizWorld
@testable import QuizWorldAPI

import Prelude
import ReactiveSwift
import ReactiveExtensions
import Result

I've tried adding alot of different imports, even for the Frameworks used in the API Framework, but nothing seems to fix this. The QW-Tests target also has the correct target dependency setup. Anyone have a clue where i can go next with this? All the code is open source in the repos linked to.

like image 762
bogen Avatar asked Jan 30 '17 15:01

bogen


People also ask

How would you integrate a custom framework in a test application in iOS?

So select your iOS App and then select the target you want to link with. Then go to the General tab and click (+) in Frameworks, Libraries, and Embedded Content section, then select your framework and click Add. Link the framework to the app.

How do I add Unit Tests to an existing Xcode project?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.


1 Answers

In your test target, you have nothing set in Test Host and Bundle loader build settings. During linking phase it basically fails (doesn't know where from) to load the symbols. Target dependencies only says what should be built before, and "Link binary with libraries" has no effect for dynamic frameworks.

Set following build settings in your test target:

Test Host: $(BUILT_PRODUCTS_DIR)/QuizWorld.app/QuizWorld
Bundle Loader: $(TEST_HOST)

The docs for the latter:

Specifies the executable that will be loading the bundle output file being linked. Undefined symbols from the bundle are checked against the specified executable like it was one of the dynamic libraries the bundle was linked with.

I tried this on your project and it works.

UPD: In your second commit "setup for first test" you have removed these lines, so you might just reverse deletion of those individual lines.

UPD2: Regarding your general project setup. Try running your app on a real device, with this setup it won't find 3rd party frameworks included from QWAPI project, as they are not copied automatically. You will need to setup "Copy frameworks" build phase, to make sure that dynamic frameworks are copied in to the app bundle.

like image 131
hybridcattt Avatar answered Oct 25 '22 04:10

hybridcattt