gpt4 book ai didi

ios - 使用灵活的单元测试快速计时器功能

转载 作者:行者123 更新时间:2023-12-01 16:12:20 25 4
gpt4 key购买 nike

我正在使用 Quick、Nimble 和 RxSwift。

我的目标是编写单元测试来测试一些带有 Timer 的功能,这些功能将在一段时间后重复执行。

我的伪类

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)
}
}

我的测试类
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)
}
}
}
}
}

我希望 executeFunction()将在 8 秒后被调用,但它从未被调用并且我的测试套件失败。

知道出了什么问题吗?

最佳答案

您应该减少 Nimble 轮询间隔,因为您的轮询每 2 秒发生一次,以将您的测试类计数器值与预期值 '1' 进行比较每 2 秒。

预计 9 秒(超时),但您的最后一次轮询在 8 秒轮询后结束。

将超时时间增加 10 秒以上或减少轮询间隔以比较超时前的预期值。

提前

您可以通过注入(inject)时间间隔或使用 RxTest 来减少您的总测试时间

关于ios - 使用灵活的单元测试快速计时器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61789977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com