【问题标题】:Swift: Timeout when unit testing orientation changes after upgrading to Xcode 10Swift:升级到 Xcode 10 后单元测试方向更改时超时
【发布时间】:2018-09-20 13:30:08
【问题描述】:

我们最近从 Xcode 8 升级到了 10。在终于重新编译所有内容后,我在单元测试中遇到了涉及方向更改的问题。 我一直在做的模拟设备方向变化是

    XCUIDevice.shared().orientation = .landscapeLeft
    ...
    XCUIDevice.shared().orientation = .faceUp
    ...
    etc.

现在执行此行时,“设备”(iPhone 8+ 的模拟器)正在进行适当的方向更改,但由于确认超时,调用最终失败:

    [iMomTests.DataScreenTests testLandscape] : Failed to set orientation: Error Domain=XCTDaemonErrorDomain Code=15 "Timed out waiting for confirmation of orientation change." UserInfo={NSLocalizedDescription=Timed out waiting for confirmation of orientation change.}

【问题讨论】:

  • 我也看到了这一点,它使我们无法迁移到 Xcode 10,因为它破坏了我们的单元测试。这能被抓住和处理吗?看起来不像。
  • 另外,在第一次失败后,对景观的后续更改会成功且不会出现错误。

标签: swift unit-testing timeout orientation xcode10


【解决方案1】:

这是我根据上面提到的KIF的旋转方法想出的一个扩展。

public extension XCTestCase {
    public func waitUntilRotation(to orientation: UIInterfaceOrientation) {
        guard UIApplication.shared.statusBarOrientation != orientation else {
            return
        }
        UIDevice.current.setValue(orientation.deviceOrientation.rawValue, forKey:"orientation")
    }
}

然后,在测试用例中,您在开始时调用此方法以确保方向是您所期望的。

public func testSomethingInPortrait() {
    waitUntilRotation(.portrait)
    ...
}

【讨论】:

  • 你能提供一个实现和更多的上下文吗?
【解决方案2】:

我们最终扩展了 KIFSystemTestActor 并定义了一个旋转函数:

func rotate(to orientation: UIDeviceOrientation) {
    self.simulateDeviceRotation(to: orientation)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多