如何对 Swift 定时器控制器进行单元测试?

Nic*_*hrn 2 unit-testing timer mocking swift

我正在工作一个将利用 SwiftTimer类的项目。我的TimerController班级将Timer通过启动、暂停、恢复和重置实例来控制它。

TimerController 由以下代码组成:

internal final class TimerController {

    // MARK: - Properties

    private var timer = Timer()
    private let timerIntervalInSeconds = TimeInterval(1)
    internal private(set) var durationInSeconds: TimeInterval

    // MARK: - Initialization

    internal init(seconds: Double) {
        durationInSeconds = TimeInterval(seconds)
    }

    // MARK: - Timer Control

    // Starts and resumes the timer
    internal func startTimer() {
        timer = Timer.scheduledTimer(timeInterval: timerIntervalInSeconds, target: self, selector: #selector(handleTimerFire), userInfo: nil, repeats: true)
    }

    internal func pauseTimer() {
        invalidateTimer()
    }

    internal func resetTimer() {
        invalidateTimer()
        durationInSeconds = 0
    }

    // MARK: - Helpers

    @objc private func handleTimerFire() {
        durationInSeconds += 1
    }

    private func invalidateTimer() {
        timer.invalidate()
    }

}
Run Code Online (Sandbox Code Playgroud)

目前,我的TimerControllerTests包含以下代码:

class TimerControllerTests: XCTestCase {

    func test_TimerController_DurationInSeconds_IsSet() {
        let expected: TimeInterval = 60
        let controller = TimerController(seconds: 60)
        XCTAssertEqual(controller.durationInSeconds, expected, "'durationInSeconds' is not set to correct value.")
    }

}
Run Code Online (Sandbox Code Playgroud)

我能够测试在初始化TimerController. 但是,我不知道从哪里开始测试TimerController.

我想,以确保班级成功处理startTimer()pauseTimer()resetTimer()。我希望我的单元测试尽可能快地运行,但我认为我需要实际启动、暂停和停止计时器来测试durationInSeconds在调用适当的方法后属性是否更新。

TimerController在我的单元测试中实际创建计时器并调用方法以验证durationInSeconds是否已正确更新是否合适?

我意识到它会减慢我的单元测试速度,但我不知道另一种适当测试此类及其预期操作的方法。

更新

我一直在做一些研究,我发现,就我的测试而言,我认为这是一个似乎可以完成工作的解决方案。但是,我不确定这种实现是否足够。

我重新实现了我的TimerController如下:

internal final class TimerController {

    // MARK: - Properties

    private var timer = Timer()
    private let timerIntervalInSeconds = TimeInterval(1)
    internal private(set) var durationInSeconds: TimeInterval
    internal var isTimerValid: Bool {
        return timer.isValid
    }

    // MARK: - Initialization

    internal init(seconds: Double) {
        durationInSeconds = TimeInterval(seconds)
    }

    // MARK: - Timer Control

    internal func startTimer(fireCompletion: (() -> Void)?) {
        timer = Timer.scheduledTimer(withTimeInterval: timerIntervalInSeconds, repeats: true, block: { [unowned self] _ in
            self.durationInSeconds -= 1
            fireCompletion?()
        })
    }

    internal func pauseTimer() {
        invalidateTimer()
    }

    internal func resetTimer() {
        invalidateTimer()
        durationInSeconds = 0
    }

    // MARK: - Helpers

    private func invalidateTimer() {
        timer.invalidate()
    }

}
Run Code Online (Sandbox Code Playgroud)

此外,我的测试文件已通过测试:

class TimerControllerTests: XCTestCase {

    // MARK: - Properties

    var timerController: TimerController!

    // MARK: - Setup

    override func setUp() {
        timerController = TimerController(seconds: 1)
    }

    // MARK: - Teardown

    override func tearDown() {
        timerController.resetTimer()
        super.tearDown()
    }

    // MARK: - Time

    func test_TimerController_DurationInSeconds_IsSet() {
        let expected: TimeInterval = 60
        let timerController = TimerController(seconds: 60)
        XCTAssertEqual(timerController.durationInSeconds, expected, "'durationInSeconds' is not set to correct value.")
    }

    func test_TimerController_DurationInSeconds_IsZeroAfterTimerIsFinished() {
        let numberOfSeconds: TimeInterval = 1
        let durationExpectation = expectation(description: "durationExpectation")
        timerController = TimerController(seconds: numberOfSeconds)
        timerController.startTimer(fireCompletion: nil)
        DispatchQueue.main.asyncAfter(deadline: .now() + numberOfSeconds, execute: {
            durationExpectation.fulfill()
            XCTAssertEqual(0, self.timerController.durationInSeconds, "'durationInSeconds' is not set to correct value.")
        })
        waitForExpectations(timeout: numberOfSeconds + 1, handler: nil)
    }

    // MARK: - Timer State

    func test_TimerController_TimerIsValidAfterTimerStarts() {
        let timerValidityExpectation = expectation(description: "timerValidity")
        timerController.startTimer {
            timerValidityExpectation.fulfill()
            XCTAssertTrue(self.timerController.isTimerValid, "Timer is invalid.")
        }
        waitForExpectations(timeout: 5, handler: nil)
    }

    func test_TimerController_TimerIsInvalidAfterTimerIsPaused() {
        let timerValidityExpectation = expectation(description: "timerValidity")
        timerController.startTimer {
            self.timerController.pauseTimer()
            timerValidityExpectation.fulfill()
            XCTAssertFalse(self.timerController.isTimerValid, "Timer is valid")
        }
        waitForExpectations(timeout: 5, handler: nil)
    }

    func test_TimerController_TimerIsInvalidAfterTimerIsReset() {
        let timerValidityExpectation = expectation(description: "timerValidity")
        timerController.startTimer {
            self.timerController.resetTimer()
            timerValidityExpectation.fulfill()
            XCTAssertFalse(self.timerController.isTimerValid, "Timer is valid")
        }
        waitForExpectations(timeout: 5, handler: nil)
    }

}
Run Code Online (Sandbox Code Playgroud)

我唯一能想到的使测试更快的方法是让我模拟课程并更改let timerIntervalInSeconds = TimeInterval(1)private let timerIntervalInSeconds = TimeInterval(0.1).

模拟课程以便我可以使用较小的时间间隔进行测试是否太过分了?

Jon*_*eid 8

我们可以验证对测试替身的调用,而不是使用真正的计时器(这会很慢)。

挑战在于代码调用了工厂方法Timer.scheduledTimer(…). 这会锁定依赖项。如果测试可以提供模拟计时器,则测试会更容易。

通常,注入工厂的一个好方法是提供一个闭包。我们可以在初始化程序中执行此操作,并提供一个默认值。然后,默认情况下,闭包将实际调用工厂方法。

在这种情况下,它有点复杂,因为对Timer.scheduledTimer(…)自身的调用需要一个闭包:

internal init(seconds: Double,
              makeRepeatingTimer: @escaping (TimeInterval, @escaping (TimerProtocol) -> Void) -> TimerProtocol = {
                  return Timer.scheduledTimer(withTimeInterval: $0, repeats: true, block: $1)
              }) {
    durationInSeconds = TimeInterval(seconds)
    self.makeRepeatingTimer = makeRepeatingTimer
}
Run Code Online (Sandbox Code Playgroud)

请注意,我删除Timer了块内对except 的所有引用。其他地方都使用新定义的TimerProtocol.

self.makeRepeatingTimer是闭包属性。从 调用它startTimer(…)

现在测试代码可以提供不同的闭包:

class TimerControllerTests: XCTestCase {
    var makeRepeatingTimerCallCount = 0
    var lastMockTimer: MockTimer?

    func testSomething() {
        let sut = TimerController(seconds: 12, makeRepeatingTimer: { [unowned self] interval, closure in
            self.makeRepeatingTimerCallCount += 1
            self.lastMockTimer = MockTimer(interval: interval, closure: closure)
            return self.lastMockTimer!
        })

        // call something on sut

        // verify against makeRepeatingTimerCallCount and lastMockTimer
    }
}
Run Code Online (Sandbox Code Playgroud)