Last active
August 16, 2022 03:07
-
-
Save ccabanero/6ef47c1aeb21acfb326d30f6b01825d1 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with a ViewController composed of CollectionViews
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
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! ViewController | |
//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() | |
} | |
func testSUT_CanInstantiateViewController() { | |
XCTAssertNotNil(systemUnderTest) | |
} | |
func testSUT_CollectionViewIsNotNilAfterViewDidLoad() { | |
XCTAssertNotNil(systemUnderTest.collectionView) | |
} | |
func testSUT_HasItemsForCollectionView() { | |
XCTAssertNotNil(systemUnderTest.items) | |
} | |
func testSUT_ShouldSetCollectionViewDataSource() { | |
XCTAssertNotNil(systemUnderTest.collectionView.dataSource) | |
} | |
func testSUT_ConformsToCollectionViewDataSource() { | |
XCTAssert(systemUnderTest.conformsToProtocol(UICollectionViewDataSource)) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:numberOfItemsInSection:)))) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:cellForItemAtIndexPath:)))) | |
} | |
func testSUT_ShouldSetCollectionViewDelegate() { | |
XCTAssertNotNil(systemUnderTest.collectionView.delegate) | |
} | |
func testSUT_ConformsToCollectionViewDelegate() { | |
XCTAssert(systemUnderTest.conformsToProtocol(UICollectionViewDelegate)) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:didSelectItemAtIndexPath:)))) | |
} | |
func testSUT_ConformsToCollectionViewDelegateFlowLayout () { | |
XCTAssert(self.systemUnderTest.conformsToProtocol(UICollectionViewDelegateFlowLayout)) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:layout:sizeForItemAtIndexPath:)))) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:layout:insetForSectionAtIndex:)))) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:layout:minimumLineSpacingForSectionAtIndex:)))) | |
XCTAssertTrue(systemUnderTest.respondsToSelector(#selector(systemUnderTest.collectionView(_:layout:minimumInteritemSpacingForSectionAtIndex:)))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment