How to unit test Firebase (ios) correctly?

I'm a bit stuck on how to test my approach to persisting and migrating data. I created a second Firebase project for my unit tests (although not ideal).

This is one of my tests:

func testMigratenOfFiveEvents() {
        var done = false
        let ref = FIRDatabase.database().reference()
        createFTEvents(amount: 5)
        let events = MockDatastore.shared.eventsForLife()
        FIRUtil.migrateEventsToFirebase(ftevents: events!)
        let userRecords = ref.child("ft-records").child("test@gmail_com")
        userRecords.queryOrdered(byChild: "time").observe(FIRDataEventType.value, with: { snapshot in
            var i = 0
            for item in snapshot.children {
                i = i + 1
                let event = Event(snapshot: item as! FIRDataSnapshot)
                XCTAssertEqual(event.notes, "hello" + String(i))
            }
            done = true
        })
        waitUntil(timeout: 5, predicate: {done})
    }

    func waitUntil(timeout: TimeInterval, predicate:((Void) -> Bool)) {
        let timeoutTime = NSDate(timeIntervalSinceNow: timeout).timeIntervalSinceReferenceDate

        while (!predicate() && NSDate.timeIntervalSinceReferenceDate < timeoutTime) {
            RunLoop.current.run(mode: RunLoopMode.defaultRunLoopMode, before: NSDate(timeIntervalSinceNow: 0.25) as Date)
        }
        if (!predicate() && NSDate.timeIntervalSinceReferenceDate > timeoutTime) {
            XCTAssert(false, "Timeout - Couldn't connect to firebase database")
        }
    }

      

It waits 5 seconds for the test to complete and asserts accordingly. It works well when the laptop is connected to the internet. But if it's not I get a timeout exception.

This surprises me because in Appdelete I have the following entry:

FIRDatabase.database().persistenceEnabled = true

      

According to the docs, this should work:

Firebase apps remain responsive even offline because the Firebase SDK Realtime Database saves your data to disk. Once the connection is restored, the client device gets any changes it missed, syncing it to the current state of the server.

+3


source to share





All Articles