How to test Timer in Swift Quick and Nimble

Viewed 3

I am trying to run unit test case for timer as defined below where timer is initialised in CPTimer

var searchTimer: TimerType?

func initialTimer() {
    searchTimer = CPTimer(timeInterval: 2.0, repeats: false) { [weak self] in
        self?.notifySearchResult(for: self?.searchQuery ?? "")
    }
}

Unit test case using Quick & Nimble

context("to set search timer") {
    beforeEach {
        timerStub = CPTimerStub(
            timeInterval: 2.0,
            repeats: false) {
                // Don't need to use in testing
        }
        subject.searchTimer = timerStub
    }
    it("check search timer is set") {
        subject.initialTimer()
        waitUntil(timeout: DispatchTimeInterval.milliseconds(3_000)) { done in
            expect(subject.searchController.searchResultsController)
                .to(beAKindOf(NCDeviceSearchResultController.self))
            done()
        }
        expect(subject.searchTimer).toNot(beNil())
        expect(timerStub.cpTimeInterval) == 2
        expect(timerStub.cpRepeats) == false
        expect(timerStub.cpBlock).toNot(beNil())
    }
}

But always getting failed to cover as sowing red

enter image description here

0 Answers
Related