我有一个不需要每隔10秒调用一次的函数。每次调用该功能时,我都会将定时器重置为10秒。
class MyClass {
var timer:Timer?
func resetTimer() {
self.timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) {
(timer) -> Void in
self.performAction()
}
}
func performAction() {
// perform action, then
self.resetTimer()
}
}
我想测试调用performAction()手动将计时器重置为10秒,但我似乎找不到任何好办法。存根resetTimer()感觉就像测试不会真正告诉我足够的功能。我错过了什么吗?
XCTest:
func testTimerResets() {
let myObject = MyClass()
myObject.resetTimer()
myObject.performAction()
// Test that my timer has been reset.
}
谢谢!
如果你想等待定时器触发,你仍然需要使用期望(或Xcode 9的新异步测试API)。
问题是你究竟要测试的是什么。你可能不想只是测试定时器被触发,而是你想测试定时器的处理程序实际上在做什么。 (据推测,你有一个计时器,以便执行一些有意义的事情,这就是我们应该测试的东西。)
WWDC 2017视频Engineering for Testability提供了一个很好的框架,可以考虑如何为单元测试设计代码,这需要:
那么,您的测试的输入是什么?而且,更重要的是,产出是什么。您想在单元测试中测试哪些断言?
该视频还展示了一些实际示例,说明如何通过明智地使用以下内容来重构代码以实现此结构:
在不知道计时器实际在做什么的情况下,很难进一步建议。也许你可以编辑你的问题并澄清。
首先,我会说当你没有任何名为refreshTimer
的成员时,我不知道你的对象是如何工作的。
class MyClass {
private var timer:Timer?
public var starting:Int = -1 // to keep track of starting time of execution
public var ending:Int = -1 // to keep track of ending time
init() {}
func invoke() {
// timer would be executed every 10s
timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(performAction), userInfo: nil, repeats: true)
starting = getSeconds()
print("time init:: \(starting) second")
}
@objc func performAction() {
print("performing action ... ")
/*
say that the starting time was 55s, after 10s, we would get 05 seconds, which is correct. However for testing purpose if we get a number from 1 to 9 we'll add 60s. This analogy works because ending depends on starting time
*/
ending = (1...9).contains(getSeconds()) ? getSeconds() + 60 : getSeconds()
print("time end:: \(ending) seconds")
resetTimer()
}
private func resetTimer() {
print("timer is been reseted")
timer?.invalidate()
invoke()
}
private func getSeconds()-> Int {
let seconds = Calendar.current.component(.second, from: Date())
return seconds
}
public func fullStop() {
print("Full Stop here")
timer?.invalidate()
}
}
测试(评论中的解释)
let testObj = MyClass()
// at init both starting && ending should be -1
XCTAssertEqual(testObj.starting, -1)
XCTAssertEqual(testObj.ending, -1)
testObj.invoke()
// after invoking, the first member to be changed is starting
let startTime = testObj.starting
XCTAssertNotEqual(startTime, -1)
/*
- at first run, ending is still -1
- let's for wait 10 seconds
- you should use async method, XCTWaiter and expectation here
- this is just to give you a perspective or way of structuring your solution
*/
DispatchQueue.main.asyncAfter(deadline: .now() + 10 ) {
let startTimeCopy = startTime
let endingTime = testObj.ending
XCTAssertNotEqual(endingTime, -1)
// take the difference between start and end
let diff = endingTime - startTime
print("diff \(diff)")
// no matter the time, diff should be 10
XCTAssertEqual(diff, 10)
testObj.fullStop()
}
这不是最好的方法,但它给你的观点或流程你应该如何实现这一点:)
我最终存储了原始Timer的fireDate,然后检查是否在执行操作后将新fireDate设置为比原始fireDate更晚的时间。
func testTimerResets() {
let myObject = MyClass()
myObject.resetTimer()
let oldFireDate = myObject.timer!.fireDate
myObject.performAction()
// If timer did not reset, these will be equal
XCTAssertGreaterThan(myObject.timer!.fireDate, oldFireDate)
}