Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccabanero/4a1a4bfbf5fa3fbbb1070c8765c3de73 to your computer and use it in GitHub Desktop.
Save ccabanero/4a1a4bfbf5fa3fbbb1070c8765c3de73 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with NavigationItems
// example unit tests for UIBarButtonItem target - action pattern
import XCTest
@testable import to
class ViewControllerTest: XCTestCase {
var systemUnderTest: ViewController!
override func setUp() {
super.setUp()
//get the storyboard the ViewController under test is inside
let storyboard: UIStoryboard = UIStoryboard(name: "Main_iPhone", bundle: nil)
//get the ViewController we want to test from the storyboard (note the identifier is the id explicitly set in the identity inspector)
systemUnderTest = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! PlaceListViewController
//load view hierarchy
_ = systemUnderTest.view
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
// navigation bar
func testSUT_HidesBackBarButton() {
XCTAssertTrue(systemUnderTest.navigationItem.hidesBackButton)
}
func testSUT_HasTitleView() {
XCTAssertNotNil(systemUnderTest.navigationItem.titleView)
}
func testSUT_HasTitleView_NamedPlaces() {
let centerView = systemUnderTest.navigationItem.titleView
for subview in centerView!.subviews as [UIView] {
if let label = subview as? UILabel {
XCTAssertEqual(label.text, "Places")
}
}
}
func testSUT_HasRightBarButtonItem() {
XCTAssertNotNil(self.systemUnderTest.navigationItem.rightBarButtonItem)
}
func testSUT_HasRightBarButtonItem_WithTarget_CorrectlyAssigned() {
if let rightBarButtonItem = self.systemUnderTest.navigationItem.rightBarButtonItem {
XCTAssertNotNil(rightBarButtonItem.target)
XCTAssert(rightBarButtonItem.target === self.systemUnderTest)
}
else {
XCTAssertTrue(false)
}
}
func testSUT_HasRightBarButtonItem_WithActionMethod_CorrectlyAssigned() {
if let rightBarButtonItem = self.systemUnderTest.navigationItem.rightBarButtonItem {
XCTAssertTrue(rightBarButtonItem.action.description == "handleRightBarButtonTap:")
}
else {
XCTAssertTrue(false)
}
}
func testSUT_CanDismissViewController_WhenRightBarButtonTapped() {
class MockNavigationController: UINavigationController {
var popViewControllerIsCalled = false
override private func popViewControllerAnimated(animated: Bool) -> UIViewController? {
popViewControllerIsCalled = true
return UIViewController()
}
}
// set up the SUT as the root view controller of the mocked UINavigationController
let mockNavigationController = MockNavigationController()
mockNavigationController.setViewControllers([systemUnderTest], animated: false)
// simulate the user tapping the 'Cancel' right bar button item
systemUnderTest.handleRightBarButtonTap(UIBarButtonItem())
// confirm that the parent UINavigationController pops the SUT from the navigation stack
XCTAssertEqual(mockNavigationController.popViewControllerIsCalled, true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment