Include an Xcode 7 UI testing dependency via Cocoapods?

2.9k views Asked by At

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?

3

There are 3 answers

2
Robert Atkins On

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
1
Billy Tobon On

Seems to work using CocoaPods 0.38.0.beta.2, check https://github.com/CocoaPods/CocoaPods/issues/3709

0
funkybro On

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