XCT Testing of IBActions and IBOutlets Swift (Optionals) -


i wondering best way test iboutlets , ibactions in swift due ibactions being optionals.

i rewriting project wrote in swift time want use xct unit testing.

i have run small problem regarding testing of iboutlets , ibactions. here code:

override func setup() {     super.setup()      storyboard = uistoryboard(name: "main", bundle: nil)     mainmenuviewcontroller = storyboard.instantiateviewcontrollerwithidentifier("mainmenuviewcontroller") as! mainmenuviewcontroller      let dummy = mainmenuviewcontroller.view // force loading subviews , setting outlets      mainmenuviewcontroller.viewdidload() }  override func teardown() {     // put teardown code here. method called after invocation of each test method in class.     super.teardown() }  func testwhethertranslationgamemodebuttonexists() {     xctassert(mainmenuviewcontroller.translatebutton != nil, "translate button should exist @ times"); }  func testwhethertranslategamemodebuttontitleiscorrect() {      let title: string? = mainmenuviewcontroller.translatebutton?.titlelabel?.text      if let unwrappedtitle = title {         xctasserttrue(unwrappedtitle == "translate mode", "title of translate button should translate mode")     }     else {         xctfail("value isn't set")     } }  func testtranslategamemodebuttonibaction() {      if let unwrappedbutton = mainmenuviewcontroller.translatebutton {         if let actions: array<string> = unwrappedbutton.actionsfortarget(mainmenuviewcontroller, forcontrolevent: uicontrolevents.touchupinside) as? array<string> {              contains(actions, "")             xctasserttrue(contains(actions, "translatebuttonpressed"), "translate button should call method translatebuttonpressed")         }         else {             xctfail("button set ibaction not set")         }     }     else {         xctfail("button isn't set , therefore ibaction can not function")     } } 

as can see, first test checks see whether translate game mode button exists. works fine.

however title test need unwrap button , test label. seems 2 tests in one.

i have same issue when comes ibaction. need unwrap optional type use button , have xctassert success , failure. therefore every time use button need unwrap before can use it. seems repetitive , again seems multiple tests in one.

i wondering if correct approach, or there better way approach testing of optionals.

if possible, go xcode 7. using xcode 7, can ui tests:

file -> new -> target -> ios -> test ios ui testing bundle

the generated code has hints how use it. place cursor in test method , hit red record button.

so can simulate (almost) user action. generated code more readable, when name ui elements accessibility.

your test result way more meaningful, since testing controllers in natural habitat: inside application.

(it has been possible before xcode 7 xcode 7 it's easy do)


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -