Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing With Swift and Obj-C

I'm starting a new Swift project and I'm trying to create unit tests for it. I added the Google Analytics framework to the project and linked SystemConfiguration, CoreData, libsqlite3, libz, and libGoogleAnalyticsServices.

I then had to manually create a bridging header and added the GA headers I was going to use immediately. The app was up and running and posting to GA. I then tried to add some unit tests.

Once this happens I receive an error in my bridging header that 'GAI.h' file not found from the test target if I add a bridging header to it. I also receive a Segmentation Fault 11 error from the compiler. The error remains the same without a bridging header.

I have tried linking my test target with SystemConfiguration, CoreData, libsqlite3, libz, and libGoogleAnalyticsServices. This does not get rid of the error.

There is not much to my bridging header at the moment.

#import "GAI.h"
#import "GAILogger.h"
#import "GAITrackedViewController.h"
#import "GAIFields.h"

I'm also using cocoapods but I'm not using it with Google Analytics at the moment since there was so much I needed to manually change in the config files every time I would run the pod process. If it helps here is my pod file:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1'

pod 'JVFloatLabeledTextField'

# Swift Pods
pod 'Alamofire'
pod 'JSONHelper'

target 'example' do

end

target 'exampleTests' do

pod 'Quick', :git => "https://github.com/Quick/Quick"
pod 'Nimble', :git => "https://github.com/Quick/Nimble"

end

I haven't been able to write any tests yet because I'm not able to get passed the linker errors. Any ideas?

like image 280
James_Andreww Avatar asked Feb 10 '15 15:02

James_Andreww


People also ask

Can you use Swift and Objective-C together?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

How do you write a unit test case in Swift?

To create new unit case in iOS, go to File -> New -> File, and then select Unit Test Case Class. Doing so creates a template just like the one you got with your project. In our case, we want to name the file to correspond with the new Pokemon-related data structures we have introduced.

Does C have unit testing?

One unit testing framework in C is Check; a list of unit testing frameworks in C can be found here and is reproduced below. Depending on how many standard library functions your runtime has, you may or not be able to use one of those.


1 Answers

As stated in my comment above, I think I experienced the same or a similar issue: my code worked fine when I ran it, but when I tried to run tests, I got a Segfault 11 on trying to instantiate an object that referenced anything from a cocoa pod. I've solved it in my case.

When I had the error, my Podfile looked like this:

pod 'ReactiveCocoa'

target 'MyTests' do

use_frameworks!

pod 'Quick'

pod 'Nimble

end

use_frameworks! was the culprit: because use_frameworks! only applied to the testing target, I ended up linking to ReactiveCocoa statically when building for the normal target, and dynamically in the test target. I was missing some ReactiveCocoa imports that were required only when linking dynamically, but instead of the compiler telling me that it segfaulted.

My Podfile now looks like this:

use_frameworks!

pod 'ReactiveCocoa'

target 'MyTests' do

pod 'Quick'

pod 'Nimble

end

There were a few linking issues to work out but they were easy from there, because when I compiled the main target I got sane errors. Hopefully this helps someone :)

like image 129
Michelle Ellis Avatar answered Oct 26 '22 17:10

Michelle Ellis