Can you add Firebase to unit tests in a Swift package?

Viewed 149

I have a Swift package that implements some read/write methods to Firebase. The package has Firebase correctly set up as a dependency. I can add my Swift package to an iOS project and it works correctly.

Now I want to write some unit tests for the Swift package (an XCTestCase) to ensure that the read/write operations are doing what I want.

Is that even possible - can I add Firebase to the tests of a Swift package? Where would you call FirebaseApp.configure()? Does a unit test in a swift package even have a bundle ID to generate the GoogleService-Info.plist?

1 Answers

When you initialize Firebase with a GoogleService-Info.plist file, Firebase parses that file and and the configuration is ultimately handled through a FirebaseOptions object. FirebaseApp.configure(options:) allows you to pass a custom FirebaseOptions object for configuration.

I was able to get Firebase initialized for tests by downloading the GoogleService-Info.plist file and copying the corresponding values to the following code:

final class DemoTests: XCTestCase {
    override func setUp() {
        let appOptions = FirebaseOptions(
            googleAppID: "**GOOGLESERVICE-INFO-GOOGLE-APP-ID**",
            gcmSenderID: "**GOOGLESERVICE-INFO-GCM-APP-ID**"
        )
        appOptions.apiKey = "**GOOGLESERVICE-INFO-API-KEY"
        appOptions.projectID = "**GOOGLESERVICE-INFO-PROJECT-ID"
        FirebaseApp.configure(options: appOptions)
    }

    func testSetup() throws { }
}
Related