Is anyone know, in unit testing, how can we test buttons, function and tableView in Swift 3.
Example I have a button action:
@IBAction func BellAction(_ sender: Any) {
let searchVC = self.storyboard?.instantiateViewController(withIdentifier:StoryBoardIDs.Notification.rawValue)
self.navigationController?.pushViewController(searchVC!, animated: true)
}
To test this:
let home = HomeDashnboard() // This is my class
home.viewDidLoad() // test viewDidLoad()
home.BellAction() // shows an error
Unfortunately you can't directly call/manipulate your view controllers, views, etc, like that. These classes lifecycle/flow are controlled by UIKit and called, on very specific moments, during your iOS app regular execution. It would be highly error prone trying to emulate such behavior yourself :-(
But, if you want to test your app UI, I highly recommend taking a closer look into the UI Testing framework also provided by Xcode:
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.