I am using Quick, Nimble, and RxSwift.
My goal is to write unit test that test some function with Timer on it which will be executed repeatedly after some time interval.
My pseudo class
final class TestingTimerClass {
let counter: BehaviorRelay<Int> = BehaviorRelay<Int>(value: 0)
private var timer: Timer?
....
func startTimer() {
timer = Timer.scheduledTimer(
timeInterval: 8,
target: self as Any,
selector: #selector(self.executeFunction),
userInfo: nil,
repeats: true
)
}
@objc private func executeFunction() {
let currentValue = counter.value
counter.accept(currentValue + 1)
}
}
My testing class
class TestingTimerClass: QuickSpec {
override func spec() {
var testingClass: TestingTimerClass!
describe("executing") {
context("startTimer()") {
beforeEach {
testingClass = TestingTimerClass()
}
afterEach {
testingClass = nil
}
it("should update counter value after a period of time") {
testingClass.startTimer()
expect(testingClass.counter.value).toEventually(equal(1), timeout: TimeInterval(9), pollInterval: TimeInterval(2), description: nil)
}
}
}
}
}
I expect that executeFunction()
will be called after 8 second, however it is never called and my test suite is failed.
Any idea what went wrong?