32

I'm writing an app in Swift, using XCode 6 Beta-6. I'm using Cocoapods and I'm creating some unit tests.

The issue is this one: apparently is not possible to have a project that contains:

1) Project written in Swift

2) Some pods installed using cocoapods

3) A Objective-C bridge header file that imports some pods

4) Unit tests

This sounds weird, but follow my steps: after running pod install, create the Objective-C bridge header and import one pod: everything works. Now write some tests: in order to test your own classes, you have to import the module called "as your project" (or better, "as the main target"): in my "MyAwesomeApp" project I have to write import MyAwesomeApp in my tests files.

Unfortunately, at this step XCode won't compile: in my import MyAwesomeApp line with the error "Failed to import bridging header '/path/to/MyAwesomeApp/MyAwesomeApp/MyAwesomeApp-Bridging-Header.h";
and the error "xxx.h file not found" appears in the Bridging-Header file, excluding the possibility to import a pod.

Also, if I don't import the pods in the Obj-c bridge file, the project will compile fine.

It looks that there is a conflict importing both the Objective-C Bridge Header (with Objective-C files taken from a different sub-project in the workspace) and the "main module" used for testing.

Do you know if there is a solution? What am I missing? Thanks

NOTE: As a workaround, I could import the pods in the Objective-C Bridge Header, and, instead of include the main module in my tests, add all the classes that I want to test in my "test" target. This will work, but it's not the most clean solution (I think)

JJSaccolo
  • 5,061
  • 2
  • 13
  • 11

3 Answers3

32

If you take a look at your main target Build Settings, you will see that there are a bunch of directories listed for the "Header Search Paths" settings.

You either need to copy those values under the test target, or you can try and modify your Podfile to include both your main and test targets and re-run install:

platform :ios, '7.0' 
link_with 'mainapp', 'mainappTests'
...

Also take care of any other header paths that could be needed and are not related to CocoaPods.

And don't forget that your classes shall have public methods wherever you want to unit-test them.

Hope this helps.

sergio
  • 68,819
  • 11
  • 102
  • 123
7

Maybe you have configured the "Objective-C Bridging Header" setting at Project level, so the "Test" target inherits that value and maybe this "Test" target is not linked with Cocoapods.

Use link_with as @sergio suggests or set the "Pods*.debug/release" configuration for the "Test" target at "Project->Info->Configuration".

Alberto Salas
  • 220
  • 4
  • 7
0

In addition to the link_with command in my Podfile i had to import my main project module in the test file. This way classes and methods don't have to public.

Note the special @testable annotation

@testable import my_tutorial_app

Also my main project name had non-alphanumerical characters in it, I had to replace them with underscores _

Kevin Goedecke
  • 1,553
  • 2
  • 14
  • 26