Forked from ccabanero/Sample iOS Unit Tests: Working with a ViewController composed of MKMapView
Last active
June 26, 2018 13:14
-
-
Save tobitech/52a7105b724913d095a5328181442013 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with a ViewController composed of MKMapView
This file contains hidden or 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 UIKit | |
import XCTest | |
import MapKit | |
class ExampleTests: XCTestCase { | |
//declaring the ViewController under test as an implicitly unwrapped optional | |
var viewControllerUnderTest : ViewController! | |
override func setUp() { | |
super.setUp() | |
//get the storyboard the ViewController under test is inside | |
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType)) | |
//get the ViewController we want to test from the storyboard (note the identifier is the id explicitly set in the identity inspector) | |
viewControllerUnderTest = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as ViewController | |
//load view hierarchy | |
if(viewControllerUnderTest != nil) { | |
viewControllerUnderTest.loadView() | |
viewControllerUnderTest.viewDidLoad() | |
} | |
} | |
func testViewControllerIsComposedOfMapView() { | |
XCTAssertNotNil(viewControllerUnderTest.mapView, "ViewController under test is not composed of a MKMapView") | |
} | |
func testControllerConformsToMKMapViewDelegate() { | |
XCTAssert(viewControllerUnderTest.conformsToProtocol(MKMapViewDelegate), "ViewController under test does not conform to MKMapViewDelegate protocol") | |
} | |
func testMapViewDelegateIsSet() { | |
XCTAssertNotNil(self.viewControllerUnderTest.mapView.delegate) | |
} | |
func testControllerImplementsMKMapViewDelegateMethods() { | |
XCTAssert(viewControllerUnderTest.respondsToSelector(Selector("mapView:viewForAnnotation:")), "ViewController under test does not implement mapView:viewForAnnotation") | |
} | |
func testMapInitialization() { | |
XCTAssert(viewControllerUnderTest.mapView.mapType == MKMapType.Hybrid); | |
} | |
func testControllerAddsAnnotationsToMapView() { | |
let annotationsOnMap = self.viewControllerUnderTest.mapView.annotations | |
XCTAssertGreaterThan(annotationsOnMap.count, 0) | |
} | |
func testControllerCanAddHospitalAnnotationToMapView() { | |
let mapHasHospitalAnno = self.hasTargetAnnotation(HospitalAnnotation) | |
XCTAssertTrue(mapHasHospitalAnno) | |
} | |
func testControllerCanAddSchoolAnnotationToMapView() { | |
let mapHasSchoolAnno = self.hasTargetAnnotation(SchoolAnnotation) | |
XCTAssertTrue(mapHasSchoolAnno) | |
} | |
// MARK: - Utility | |
func hasTargetAnnotation(annotationClass: MKAnnotation.Type) -> Bool { | |
let mapAnnotations = self.viewControllerUnderTest.mapView.annotations | |
var hasTargetAnnotation = false | |
for anno in mapAnnotations { | |
if (anno.isKindOfClass(annotationClass)) { | |
hasTargetAnnotation = true | |
} | |
} | |
return hasTargetAnnotation | |
} | |
func testMapViewCanRenderPolygonByImplementingMapViewRendererForOverlay() { | |
XCTAssert(self.viewControllerUnderTest.respondsToSelector(Selector("mapView:rendererForOverlay:"))) | |
} | |
//continue with App-specific tests ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment