|
import XCTest |
|
@testable import WeatherApp |
|
|
|
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) |
|
} |
|
} |