Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue testing and using Cocoapods in a Swift project

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)

like image 980
JJSaccolo Avatar asked Aug 25 '14 22:08

JJSaccolo


People also ask

What are the pros of using CocoaPods?

CocoaPods is a tool that makes managing your project much simpler. It can save you a lot of effort and time when dealing with dependencies in your project as it makes adding, removing and updating libraries that much easier. For more on using and troubleshooting CocoaPods, check out the CocoaPods guides.


2 Answers

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.

like image 148
sergio Avatar answered Oct 09 '22 12:10

sergio


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

like image 7
Alberto Salas Avatar answered Oct 09 '22 14:10

Alberto Salas