Last active
March 9, 2021 15:08
-
-
Save ccabanero/68cd8ff461316930f707 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with a ViewController composed of a UILabel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import UnitTests | |
class ExampleTests: XCTestCase { | |
var viewControllerUnderTest: ViewController! | |
override func setUp() { | |
super.setUp() | |
let storyboard = UIStoryboard(name: "Main", bundle: nil) | |
self.viewControllerUnderTest = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController | |
self.viewControllerUnderTest.loadView() | |
self.viewControllerUnderTest.viewDidLoad() | |
} | |
override func tearDown() { | |
super.tearDown() | |
} | |
func testViewControllerIsComposedOfLabel() { | |
XCTAssertNotNil(self.viewControllerUnderTest.label, "ViewController under test is not composed of a UILabel") | |
} | |
func testViewControllerInitializesLabelText() { | |
XCTAssert(self.viewControllerUnderTest.label.text == "Vader", "ViewController under test does not initialize the text property of UILabel correctly") | |
} | |
func testLabelAfterButtonTap() { | |
let button: UIButton = UIButton() | |
viewControllerUnderTest.handleTap(button) | |
let expectedLabelText = "Hello testing" | |
let actualLabelText = viewControllerUnderTest.label.text | |
XCTAssertEqual(expectedLabelText, actualLabelText, "Label text of: \(expectedLabelText) not equal to text: \(actualLabelText)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sAurangzaib, I have same issue. Did you solve this issue?