使用单元测试时遇到问题

Trouble when using Unit testing

有谁知道,在单元测试中,我们如何测试按钮,功能和tableView Swift 3.

示例我有一个按钮操作:

@IBAction func BellAction(_ sender: Any) {
    let searchVC = self.storyboard?.instantiateViewController(withIdentifier:StoryBoardIDs.Notification.rawValue)
    self.navigationController?.pushViewController(searchVC!, animated: true)
}

要对此进行测试:

let home = HomeDashnboard() //  This is my class 
home.viewDidLoad() // test viewDidLoad()
home.BellAction() // shows an error

不幸的是,你不能直接call/manipulate你的视图控制器视图,等等。这些 类 lifecycle/flow 由 UIKit 控制,并在您的 iOS 应用常规执行期间的特定时刻调用。尝试自己模拟这种行为很容易出错:-(

但是,如果您想测试您的应用程序 UI,我强烈建议您仔细查看 [=25] 也提供的 UI Testing framework =]:

UI testing gives you the ability to find and interact with the UI of your app in order to validate the properties and state of the UI elements.

UI testing includes UI recording, which gives you the ability to generate code that exercises your app's UI the same way you do, and which you can expand upon to implement UI tests. This is a great way to quickly get started writing UI tests.