Xctest VC层次结构没有Xcuitest?

发布于 2025-02-05 02:08:31 字数 1235 浏览 4 评论 0 原文

我想在不使用Xcuitest的情况下测试一个ViewController层次结构,因为它们的吨比普通的Xctest长。

我取得了一些成功,但我似乎无法创建一个多个级别的ViewController层次结构。

这是一个证明问题的测试案例:

class VCHierarchyTests: XCTestCase {

    func test_CanMakeViewControllerHierarchy() {

          let baseVC = UIViewController()
          let vcToPresent = UIViewController()
          let vcOnTopOfThat = UIViewController()
          let vcOnTopOfThatOnTopOfThat = UIViewController()
          newKeyWindowWith(root: baseVC)
          baseVC.present(vcToPresent, animated: false)
          vcToPresent.present(vcOnTopOfThat, animated: false)
          vcOnTopOfThat.present(vcOnTopOfThatOnTopOfThat, animated: false)

          //this passes:
          XCTAssertNotNil(baseVC.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcToPresent.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcOnTopOfThat.presentedViewController)
    }

    private func newKeyWindowWith(root viewController: UIViewController){
           let window = UIWindow(frame: UIScreen.main.bounds)
           window.makeKeyAndVisible()
           window.rootViewController = viewController
    }
}

有没有办法在没有Xcuitest的情况下构建ViewController层次结构?

I want to test a ViewController hierarchy without using XCUITests, because they take a huge ton longer than plain old XCTests.

I've had some success but I can't seem to create a ViewController hierarchy more than one level deep.

Here's a test case demonstrating the issue:

class VCHierarchyTests: XCTestCase {

    func test_CanMakeViewControllerHierarchy() {

          let baseVC = UIViewController()
          let vcToPresent = UIViewController()
          let vcOnTopOfThat = UIViewController()
          let vcOnTopOfThatOnTopOfThat = UIViewController()
          newKeyWindowWith(root: baseVC)
          baseVC.present(vcToPresent, animated: false)
          vcToPresent.present(vcOnTopOfThat, animated: false)
          vcOnTopOfThat.present(vcOnTopOfThatOnTopOfThat, animated: false)

          //this passes:
          XCTAssertNotNil(baseVC.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcToPresent.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcOnTopOfThat.presentedViewController)
    }

    private func newKeyWindowWith(root viewController: UIViewController){
           let window = UIWindow(frame: UIScreen.main.bounds)
           window.makeKeyAndVisible()
           window.rootViewController = viewController
    }
}

Is there a way to construct ViewController hierarchies without XCUITest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-02-12 02:08:31

尝试 view controllererPresentationspypy

它旨在拦截单个 present()调用,而不是其中几个。如果您的用例不在其所做的事情之外,请提出问题。

Try PresentationVerifier from ViewControllerPresentationSpy to unit test how view controllers are presented.

It's designed to intercept a single present() call, not several of them. If your use case falls outside what it does, please file an Issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文