Monday, November 13, 2017

Missing Firebase module error when adding unit test target into existing ios project

An ios xcode swift project was created a while ago with swift 3.0. Later when adding a unit test target into the project, a build error happens due to import module gets unknown module or missing Firebase module error.

For the unknown module error, it is because the module name has been changed after the project was created, the import statement requires module name instead of target name. So fix this error, just go to application's build settings' module name field and use the module name for the import statement.

The second error is due to swift version mismatch, the application target uses swift 3.0, while the new unit test target uses swift 4.0, and the Firebase library downloaded by pod file can only have one version, so both target must have the same version. From the target's Build Settings, change the swift language version to 3.0

The pod file was generated only have the configuration for application target, after adding the unit test target, it needs to be updated to also have Firebase library for unit test target as below, and then run "pod update" to update the pod

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'

target 'myapp' do
  use_frameworks!
  pod 'Firebase/Core'
  pod 'Firebase/AdMob'
  
  target 'UnitTests' do
        use_frameworks!
        inherit! :search_paths
        pod 'Firebase'
  end

end


Finally after import the application module into the test file, be sure @testable is used, so the internal class can be used by the unit test methods.

import XCTest
@testable import myapp

No comments:

Post a Comment