How do I create system integration tests using Firebase and Swift/Xcode?

Viewed 67

I want to create tests for user account creation, so I can confirm my app is uploading the correct data, but I'm struggling to find the right way to do it. So far I've been unsuccessful in writing a test that works. This error prevents me from getting any further:

caught error: "An error occurred when accessing the keychain. The NSLocalizedFailureReasonErrorKey field in the NSError.userInfo dictionary will contain more information about the error encountered"

I've not written tests in Xcode before, and I've had lots of trouble finding guides on this sort of issue. Here is my test code:

import XCTest
import Firebase
import FirebaseFirestoreSwift
import SwiftUI
@testable import Baseline

class AccountTests: XCTestCase {
    var baselineApp: BaselineApp!

    override func setUpWithError() throws {
        baselineApp = BaselineApp()
    }

    override func tearDownWithError() throws {
        async {
            let idString = baselineApp.authenticationState.loggedInUser?.uid
            if let idString = idString {
                try await baselineApp.authenticationState.deleteUser(id: idString)
            }
            baselineApp.authenticationState.signout()
        }
    }

    func testAccountCreation() async throws {
        let firstName = "Test"
        let lastName = "Xcode"
        let email = "testdummy5684+XCtest@gmail.com"
        let password = "testingForXcode1234"
        let gender = UserDocument.Gender.male
        let birthdate = Timestamp(date: Date())
        let suggestedRank = 0
        let image = UIImage(named: "social")?.jpegData(compressionQuality: 1.0)
        try await baselineApp.authenticationState.signupNewUser(email: email, password: password, firstName: firstName, lastName: lastName, gender: gender, jpegImageData: image, suggestedRank: suggestedRank, birthdate: birthdate)
        let foundUserResult = baselineApp.authenticationState.loggedInUser
        let foundUserDocumentResult = baselineApp.authenticationState.loggedInUserDocument
        let foundUser = try XCTUnwrap(foundUserResult)
        let foundUserDocument = try XCTUnwrap(foundUserDocumentResult)
        XCTAssertNotNil(foundUserDocument, "Failed to get user document")
        XCTAssertEqual(firstName, foundUserDocument.firstName, "Tried \(firstName), but got \(foundUserDocument.firstName)")
    }
}

I'm creating an instance of my app where my @main is, because I have observable objects instantiated there, alongside Firebase's config settings. I have keychain sharing enabled, and my app works otherwise (like logging into a pre-existing account).

I'm running Xcode 13.0 beta (13A5155e) and targeting iOS 15.

0 Answers
Related