Swift IOS-测试警报UialertController

发布于 2025-02-05 19:45:14 字数 1403 浏览 2 评论 0原文

我想在警报上进行一些测试,UialertController。这就是测试的样子:

class TransactionsListViewControllerTests: XCTestCase {

   private var viewController: MockTransactionsListViewController!

   func testPresentAlertForAccountInCollection() throws {
        let fetchAlertContent = try retrieveAlertContent(mockPath: "alertContent.json", refresh: false)
        if let error = fetchAlertContent.result?.error {
            XCTFail(error.localizedDescription)
            return
        }

        let alertController = UIAlertController(
            title: fetchAlertContent.title,
            message: fetchAlertContent.description, preferredStyle: .alert
        )
        let okButton = UIAlertAction(title: fetchAlertContent.buttonTitle, style: .default)
        alertController.addAction(okButton)
        viewController.present(alertController, animated: false, completion: nil)

        let expectation = XCTestExpectation(description: "show alert")
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            XCTAssertTrue(self.viewController.presentedViewController is UIAlertController)
   expectation.fulfill()
        }
   }
}

如何正确测试所呈现的视图的uialertController的正确方法?该测试在我的本地存储库上正常工作,但是当该测试在我们的工作领域(TeamCity)上时,它面临一些问题并在同一模块中引用另一个测试。

XCTASSERTTRUE似乎正在同一模块中测试另一个快照测试,并且我在GitHub/TeamCity上遇到了问题,说XCTASSERTTRUE在另一个测试文件中失败了。我想知道如何改进此测试,以便它在此文件中引用。希望这有意义,可以提供更多的澄清

I'd like to do some testing on an Alert, UIAlertController. This is what the testing looks like:

class TransactionsListViewControllerTests: XCTestCase {

   private var viewController: MockTransactionsListViewController!

   func testPresentAlertForAccountInCollection() throws {
        let fetchAlertContent = try retrieveAlertContent(mockPath: "alertContent.json", refresh: false)
        if let error = fetchAlertContent.result?.error {
            XCTFail(error.localizedDescription)
            return
        }

        let alertController = UIAlertController(
            title: fetchAlertContent.title,
            message: fetchAlertContent.description, preferredStyle: .alert
        )
        let okButton = UIAlertAction(title: fetchAlertContent.buttonTitle, style: .default)
        alertController.addAction(okButton)
        viewController.present(alertController, animated: false, completion: nil)

        let expectation = XCTestExpectation(description: "show alert")
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            XCTAssertTrue(self.viewController.presentedViewController is UIAlertController)
   expectation.fulfill()
        }
   }
}

What would be the correct way to test that the UIAlertController that is the presented view? The test works fine on my local repository, but when this test is on our work domain (TeamCity), it's facing some issues and referencing another Test in the same module.

The XCTAssertTrue appears to be testing another SnapShot test within the same module and I'm getting issues on GitHub/TeamCity saying that XCTAssertTrue failed with a different test file. I'd like to know how I can improve this testing so that it reference within this file. Hopefully that makes sense, can provide more clarification

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文