Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include an Xcode 7 UI testing dependency via Cocoapods?

I have an existing Objective-C project and I want to add a new Xcode 7 UI testing target with OHHTTPStubs as a dependency.

I added the new (Swift 2.0) UI testing target in Xcode, then added this to my Podfile:

target 'FooUITests' do
    pod 'OHHTTPStubs', '4.0.1'
end

I ran pod update, cleaned, and rebuilt. But when I try and import OHHTTPStubs at the top of the template UI test .swift file Xcode created for me, it complains "No such module 'OHHTTPStubs'".

I'm using Cocoapods version 0.37.2—is importing an Objective-C dependency into a Swift (... UI test) target even meant to work?

UPDATE: As noted in my self-answer below, adding use_frameworks! to my Podfile gets me clean compilation—I can import OHHTTPStubs at the top of my test file, reference classes and methods, code completion works—but when I actually go to run the tests I get the following output in the Xcode console:

2015-06-18 10:06:57.134 XCTRunner[51557:609693] The bundle “FooUITests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2015-06-18 10:06:57.135 XCTRunner[51557:609693] (dlopen_preflight(/Users/foo/Library/Developer/CoreSimulator/Devices/38181A1B-67B1-4D7F-B434-85361533F985/data/Containers/Bundle/Application/83C68748-55A3-4A02-8862-C18ADEF895B5/FooUITests-Runner.app/PlugIns/FooUITests.xctest/FooUITests): Library not loaded: @rpath/OHHTTPStubs.framework/OHHTTPStubs
  Referenced from: /Users/foo/Library/Developer/CoreSimulator/Devices/38181A1B-67B1-4D7F-B434-85361533F985/data/Containers/Bundle/Application/83C68748-55A3-4A02-8862-C18ADEF895B5/FooUITests-Runner.app/PlugIns/FooUITests.xctest/FooUITests
  Reason: image not found)

There do seem to be Release-iphoneos and Release-iphonesimulator builds of the OHHTTPStubs.framework under my ~/Library/Developer/DerivedData directory though.

Any hints as to what is going on?

like image 248
Robert Atkins Avatar asked Jun 17 '15 13:06

Robert Atkins


People also ask

What are CocoaPods in Xcode?

CocoaPods is an open-source dependency manager for Xcode projects. When you develop a mobile app for iOS, you can use CocoaPods to define all of your dependencies in a simple text file, called a Podfile.

How do you manage dependencies in Xcode?

X depends on Y, which depends on Z. Manage dependencies with CocoaPods. CocoaPods is an open-source dependency manager for Xcode projects. When you develop a mobile app for iOS, you can use CocoaPods to define all of your dependencies in a simple text file, called a Podfile.

What is the use of CocoaPods?

CocoaPods is an open-source dependency manager for Xcode projects. When you develop a mobile app for iOS, you can use CocoaPods to define all of your dependencies in a simple text file, called a Podfile. CocoaPods does these tasks:


Video Answer


2 Answers

Turns out all I needed to do was tell Cocoapods to use_frameworks! (for the Swift target only) in the Podfile:

target 'FooUITests' do
  use_frameworks!
  pod 'OHHTTPStubs', '4.0.1'
end
like image 66
Robert Atkins Avatar answered Sep 22 '22 00:09

Robert Atkins


Adding a [CP] Embed Pods Frameworks Run Script build phase to my test target fixed this for me, as described in this CocoaPods GitHub issue.

Note that in your regular target, your Build Phases section contains both [CP] Copy Pods Resources (which runs "${SRCROOT}/../../Pods/Target Support Files/Pods-YOURTARGET/Pods-YOURTARGET-resources.sh"), and [CP] Embed Pods Frameworks (which runs "${SRCROOT}/../../Pods/Target Support Files/Pods-YOURTARGET/Pods-YOURTARGET-frameworks.sh"). But your test target contains only [CP] Copy Pods Resources.

Manually add the [CP] Embed Pods Frameworks Run Script phase to your test target (to run "${SRCROOT}/../../Pods/Target Support Files/Pods-YOURTESTTARGET/Pods-YOURTESTTARGET-resources.sh").

like image 42
funkybro Avatar answered Sep 25 '22 00:09

funkybro