I am writing unit test cases for the view controllers in my iOS app. I am trying to test if the UI elements involving IBOutlets are not nil as in the code below.
class ClientsViewControllerTests: XCTestCase {
var clientsVC: ClientsTableViewController?
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Clients", bundle: nil)
clientsVC = storyboard.instantiateInitialViewController() as? ClientsTableViewController
if let vc = clientsVC?{
vc.loadView()
}
}
override func tearDown() {
super.tearDown()
clientsVC = nil
}
func testClientViewControllerNotNil(){
XCTAssertNotNil(clientsVC, "view controller cannot be nil")
}
I test fails and outputs "view controller cannot be nil"
I fail to understand why.However, the below test passes:
func testStoryBoard(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("MainVC") as UIViewController
XCTAssertNotNil(vc,"Storyboard is not connected with a viewcontroller")
But I need to do it in the 1st method as I want to test for the IBOutlet bindings for the Specific viewController like:
XCAssertNotNil(vc.sendButton, "Send button is nil")
Kindly guide me why the test is failing and also how to test outlet binding and the action binding in ViewControllers
Aucun commentaire:
Enregistrer un commentaire