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

时间:2017-01-20 03:49:58

标签: swift unit-testing timer mocking

我正在开发一个利用Swift Timer课程的项目。我的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()
    }

}

目前,我的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.")
    }

}

我可以测试初始化​​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()
    }

}

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

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

}

我能想到的唯一能让测试更快的是我嘲笑课程并将let timerIntervalInSeconds = TimeInterval(1)更改为private let timerIntervalInSeconds = TimeInterval(0.1)

模拟课程是否过度杀伤,以便我可以使用较小的时间间隔进行测试?

1 个答案:

答案 0 :(得分:3)

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

挑战在于代码调用工厂方法31 32 33 d6 d0 ce c4 a ffffffff 。这会锁定依赖关系。如果测试可以提供模拟计时器,则测试会更容易。

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

在这种情况下,它有点复杂,因为对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 } 的所有引用。其他地方都使用新定义的Timer

TimerProtocol是一个闭包属性。从self.makeRepeatingTimer调用它。

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

startTimer(…)