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

何故テストが書けないのか #ios_test_night /why_we_cannot_wri...

fromkk
December 05, 2017

何故テストが書けないのか #ios_test_night /why_we_cannot_write_test

2017/12/05(火)DeNAさんで開催されたiOS Test Night #6 - 1周年 - で発表した内容です。

fromkk

December 05, 2017
Tweet

More Decks by fromkk

Other Decks in Programming

Transcript

  1. 3

  2. 4

  3. ! 9

  4. ςετத͸RealmΛInMemoryػೳΛ༗ޮʹ class DB { static var isTesting: Bool = false

    private static var userConfiguration: Realm.Configuration { return Realm.Configuration() } private static var inMemoryConfiguration: Realm.Configuration { return Realm.Configuration(inMemoryIdentifier: "InMemory") } static var realm: Realm { let configuration: Realm.Configuration if isTesting { configuration = inMemoryConfiguration } else { configuration = userConfiguration } return try! Realm(configuration: configuration) } } 17
  5. ςετ @testable import Type class RegisterViewControllerTests: XCTestCase { class MockRegistrable:

    MarkdownRegistrable { let defaultValue = "Hello world" func open(_ completed: @escaping (String) -> ()) { completed(defaultValue) } var isSaved: Bool = false func save(with markdown: String) { isSaved = true } } private func makeViewController() -> RegisterViewController { let registrable = MockRegistrable() let viewController = RegisterViewController() viewController.registrable = registrable return viewController } func testOpen() { let viewController = makeViewController() viewController.open() XCTAssetEqual(registrable.textView.text, "Hello world") } func testSave() { let viewController = makeViewController() viewController.save(with: "hoge huga") XCTAssetTrue(registrable.isSaved) } } 22
  6. 27

  7. 28

  8. 29