Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ViewControllerの単体テスト

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 ViewControllerの単体テスト

ViewControllerの単体テストができるようにどうすればいいか。

Avatar for Yoann Coldefy

Yoann Coldefy

January 14, 2017
Tweet

More Decks by Yoann Coldefy

Other Decks in Programming

Transcript

  1. func setupViewController() { let storyboard = UIStoryboard(name: "MyStoryboard", bundle: Bundle.main)

    if let viewController = storyboard.instantiateInitialViewController() as? MyViewController { target = viewController } } ී௨ʹStoryboardͳͲ͔ΒͰ͖·͢ɻ class RegisterAccountViewControllerTests: XCTestCase { // MARK: Subject under test var target: MyViewController!
  2. WindowΛ௥Ճͯ͠ɺϏϡʔΛϩʔυ͠·͢ɻ class RegisterAccountViewControllerTests: XCTestCase { // MARK: Subject under test

    var target: MyViewController! var window: UIWindow! ← New! func loadView() { window.addSubview(target.view) RunLoop.current.run(until: Date()) } ඞཁ͚ͩͲཧ༝͕Θ͔Βͳ͍
  3. ViewDidLoadͰઃఆ͞ΕΔ΋ͷ override func setUp() { dataSource = MockDataSource() target.dataSource =

    dataSource } func test_viewDidLoad_shouldSetTheTableDataSource() { // Arrange // Act loadView() // Assert XCTAssertTrue(target.tableView.dataSource === dataSource) }
  4. func test_viewDidLoad_shouldSetTheHandlersToTheTableDataSource() { // Arrange // Act loadView() // Assert

    XCTAssertNotNil(dataSource.handlers) } ViewDidLoadͰઃఆ͞ΕΔ΋ͷ
  5. protocolͷ֤ؔ਺ͷςετͰ͖·͢ func test_numberOfSections_shouldReturn6() { // Arrange // Act let numberOfSections

    = target.numberOfSections(in: tableViewController.tableView) // Assert XCTAssertEqual(numberOfSections, 6) }
  6. protocolͷ֤ؔ਺ͷςετͰ͖·͢ func test_numberOfRowsInSection_section1_shouldReturn2() { // Arrange // Act let numberOfRows

    = target.tableView(tableViewController.tableView, numberOfRowsInSection: 1) // Assert XCTAssertEqual(numberOfRows, 2) }
  7. protocolͷ֤ؔ਺ͷςετͰ͖·͢ func test_cellForRowAt_row0Section5_shouldBeButtonCell() { // Arrange let indexPath = IndexPath(row:

    0, section: 5) // Act let cell = target.tableView(tableViewController.tableView, cellForRowAt: indexPath) // Assert XCTAssertTrue(cell is ButtonCell) }