GOAL: I'm currently unit testing the behavior of my UISwitch when it is pressed.
ISSUE: My issue I that the sendActions(for: .valueChanged)
that I use to programmatically change the UISwitch's state is not working as I imagined. It doesn't make it change from false
to true
(in my example).
WHAT DID I DO ?: I'm using the following path:
- I declared my UISwitch as
true
- I used
sendActions(for: .valueChanged)
for changing it tofalse
- I finally used
XCTAssertEqual()
to check if it correctly equal tofalse
as expected. (I also could have usedXCTAssertFalse()
to check directly if it wasfalse
.
Ps: I correctly passed loadViewIfNeeded()
in my setUPWithError()
QUESTION: How can I test my UISwitch ?? Maybe using UI Tests ?
CODE: Here is my code:
var sut: ViewController!
override func setUpWithError() throws {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
sut = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
sut.loadViewIfNeeded()
}
func testGenderButton_ShouldUpdateValueWhenPressed() {
sut.genderSwitch.isOn = true
sut.genderSwitch.sendActions(for: .valueChanged)
XCTAssertEqual(sut.genderSwitch.isOn, false, "\(sut.genderSwitch.isOn) should be equal to false")
}