UITesting,XCTest当前的ViewController类 [英] UITesting, XCTest current ViewController Class

查看:158
本文介绍了UITesting,XCTest当前的ViewController类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题。我得到了对下一个视图控制器执行segue的按钮。
我想写UI XCTest告诉我它是否打开我想要的视图控制器。

Simple problem. I got button which perform segue to next view controller. I want to write UI XCTest to tell me did it open view controller i wanted.

推荐答案

UI测试框架无法访问您的应用程序代码,这使得实例上的类断言无法实现。您无法直接告诉屏幕上的控制器类。

The UI Testing framework doesn't have access to your applications code which makes class assertions on instances impossible. You are not able to directly tell the class of the controller which is on screen.

但是,如果您认为您的测试有点不同你可以做一个非常相似的断言。 将您的测试写成您的用户。您的用户不关心他/她是否正在查看 ItemDetailViewController ItemListTableViewController 所以你的测试也不应该。

However, if you think about your test a little differently you can make a very similar assertion. Write your tests as if you are the user. Your user doesn't care if he/she is looking at a ItemDetailViewController or a ItemListTableViewController so neither should your tests.

用户关心屏幕上的内容。标题是什么?或者,这些按钮的名称是什么?遵循该逻辑,您将根据这些项重写测试以断言,而不是编码类的名称。

The user cares what's on the screen. What's the title? Or, what are the names of these buttons? Following that logic you are rewrite your test to assert based on those items, not the name of the coded class.

例如,如果您在导航中显示控制器堆栈你可以断言标题。

For example, if you are presenting your controller in a navigation stack you can assert the title.

let app = XCUIApplication()
app.buttons["View Item"].tap()

XCTAssert(app.navigationBars["Some Item"].exists)

或者,如果屏幕以模态显示但您知道一些静态文本或按钮,请使用这些。

Or, if the screen is presented modally but you know some static text or buttons, use those.

let app = XCUIApplication()
app.buttons["View Item"].tap()

XCTAssert(app.staticTexts["Item Detail"].exists)
XCTAssert(app.buttons["Remove Item"].exists)

这篇关于UITesting,XCTest当前的ViewController类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆