ViewControllerの単体テスト

 ViewControllerの単体テスト

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

63734ceb95277fe45a25988f653e7e8f?s=128

Yoann Coldefy

January 14, 2017
Tweet

Transcript

  1. ViewControllerͷ୯ମςετ Swift 3ରԠ

  2. ࣗݾ঺հ • ໊લɿίʔυώʔϤΞϯ • ग़਎ɿGrenoble • ৬ۀɿιϑτΤΞʔΤϯδχΞʔ • ։ൃܦݧɿDelphiʢ7೥ʣɺSwiftʢ̍೥ʣ •

    Facebookɿhttps://www.facebook.com/yoann.coldefy • Twitterɿ@luinily
  3. גࣜձࣾΨϥύΰε • εϚϗΞϓϦͷ։ൃɺӡӦ • σΟϨΫλʔɺΤϯδχΞʔɺ σβΠφʔɺςελʔ • 35ਓ • web:

    https://www.glpgs.com/
  4. ΤϯδχΞʔืूதʂ

  5. • લճ͸CleanSwiftͷ঺հ • CleanSwift͸ςετۦಈ։ൃ͕جຊ • ViewControllerͷςετ͸Ͳ͏ͳͬͯΔʁ

  6. ViewControllerͱ͸Կʁ • ΞϓϦͷҰͭͷը໘Λίϯτϩʔϧ͢ΔΫϥε • Ͳ͜ʹԿΛදࣔ͢Δ • Ϣʔβ͕ૢ࡞ͨ࣌͠ʹΞϓϦ͸Ͳ͏൓Ԡ͢Δ

  7. ୯ମςετ͸ʁ • ϓϩάϥϜͷҰ෦Λࣗಈతʹςετ͢Δ • ૝ఆ͞ΕͯΔ݁Ռͱ࣮ࡍͷ݁Ռ͕ಉ͔͡Λ֬ೝ͢Δ
 
 ྫɿը໘͕දࣔ͞Εͨ࣌ʹϥϕϧͷςΩετ͕ ʮTestʯʹͳ͍ͬͯΔ͔Ͳ͏͔ɻ

  8. ͳͥ୯ମςετΛॻ͘ʁ • ग़དྷ্͕ͬͨϓϩάϥϜ͕࢖༻௨Γʹಈ࡞Λ͍ͯ͠Δ ͷΛࣗಈతʹςετͰ͖Δɻ • ϓϩάϥϜͷमਖ਼΍มߋͰಈ͔ͳ͘ͳͬͨͱ͜ΖΛൃ ݟ͠΍͘͢͢Δ • ࣮૷ͷπʔϧͷҰͭ

  9. λΠτϧ ը૾ ςΩετ Ϙλϯ ViewController title: Label
 picture: ImageView text:

    TextView button: Button
  10. ViewControllerͷ࣮ମԽͷ໰୊ • ViewController͸͍͔ͭ͘ͷViewΛ͍࣋ͬͯΔ͕ • ViewController͕ͦͷViewΛ࣮ମԽͤͯ͞Δ༁Ͱ͸ͳ͍ • ୯ମςετ༻ViewControllerͷΠϯελϯεΛ࡞ͬͨͩ ͚ͰɺϏϡʔ͸nilͷ··

  11. ςετͰViewController
 ͷ࣮ମԽ͸Ͳ͏͢Δʁ

  12. 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!
  13. ͜Ε͚ͩͩͱϏϡʔ͕
 ϩʔυ͞Εͳ͍ɻ

  14. 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()) } ඞཁ͚ͩͲཧ༝͕Θ͔Βͳ͍
  15. loadViewΛݺΜͩΒɺϏϡʔ͕ϩʔυ͞Εͯɺ viewDidLoad͕ݺ͹Ε·͢ɻ
 ςετͰ͖·͢ʂ

  16. ͲΜͳ͜ͱΛςετ͢Δʁ

  17. ViewDidLoadͰઃఆ͞ΕΔ΋ͷ override func setUp() { dataSource = MockDataSource() target.dataSource =

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

    XCTAssertNotNil(dataSource.handlers) } ViewDidLoadͰઃఆ͞ΕΔ΋ͷ
  19. • ϥϕϧͷςΩετ • αϒϏϡʔͷঢ়ଶ • ͳͲ

  20. ઃఆɺߦಈͳͲΛςετͯ͠ ࢓༷௨ΓʹͰ͖ͯΔ͔ ֬ೝͰ͖·͢

  21. Կ͔Πϕϯτ͋ͬͨ࣌ func test_textChanded_shouldCallValidateText() { // Arrange // Act dataSource.handlers?.textChangeHandler("newText") //

    Assert XCTAssertTrue(validatorSpy.validateTextCalled) }
  22. Կ͔Πϕϯτ͋ͬͨ࣌ func test_buttonPushed_labelTextShouldBe_New() { // Arrange // Act target.newButtonPush(nil) //

    Assert XCTAssertTrue(target.label.text, "New") }
  23. UITableViewControllerͷ৔߹ɺ DataSourceɺDelegateͷςετ΋Ͱ͖·͢ɻ

  24. protocolͷ֤ؔ਺ͷςετͰ͖·͢ func test_numberOfSections_shouldReturn6() { // Arrange // Act let numberOfSections

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

    = target.tableView(tableViewController.tableView, numberOfRowsInSection: 1) // Assert XCTAssertEqual(numberOfRows, 2) }
  26. 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) }
  27. UITableViewͷΦϒδΣΫτ͕ඞཁ ViewController͝ͱ࡞ͬͯɺϏϡʔΛϩʔυ͢Δ

  28. ςʔϒϧͷߏ੒ɺ
 ηϧͷछྨ΍ઃఆͳͲͷ ςετ͕Ͱ͖·͢

  29. ·ͱΊ • ViewController΍DataSourceͳͲͷςετ͸Մೳ • ςετΛॻ͘͜ͱͰ࢓༷௨Γʹಈ͍ͯΔͷࣗಈ తʹ֬ೝͰ͖Δ • ҆৺ͯ͠ϦϑΝΫτϦϯάͰ͖Δ

  30. ·ͱΊ • ࢓༷͕มΘΔͱςετͷमਖ਼͕ඞཁ • ςετΛॻ͘͜ͱ΋͔͔࣌ؒΓ·͢