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

DocC Tutorial と TCA におけるテスト機能の紹介

Aikawa
March 18, 2024

DocC Tutorial と TCA におけるテスト機能の紹介

TCAわいわいLT会の資料です。

Aikawa

March 18, 2024
Tweet

More Decks by Aikawa

Other Decks in Programming

Transcript

  1. νϡʔτϦΞϧͷߏ੒ • 4 Chapter Ͱ TCA ͷجຊʙ࣮ફతͳ஌ࣝΛ਎ʹ෇͚Δ͜ͱ͕Ͱ͖Δ • TCA 1.9.2

    ʹରԠࡁΈ (2024/03/17 ࣌఺) • GitHub Actions Ͱ push ͷ౓ʹ GitHub Pages ʹσϓϩΠ͞ΕΔঢ়ଶʹͳ͓ͬͯΓɺ ΠϕϯτͷλΠϛϯά౳ͰϒϥογϡΞοϓ͍ͯ͠Δ • DocC ʹΑΔνϡʔτϦΞϧ࡞੒ͷࢀߟʹͳΔ͔΋ • `Sources/Docs` ʹશͯͷϑΝΠϧ͕͋Γ·͢ • Xcode 15 ͷ DocC Preview ͕ͳ͚Ε͹࡞Δͷ͕ݫ͔ͬͨ͠…
  2. The testability of features built in the Composable Architecture is

    the #1 priority of the library. https://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/testing/
  3. ໢ཏੑ (Exhaustivity) ͷ͋Δςετ͕Մೳ let store = TestStore(initialState: Feature.State(count: 0)) {

    Feature() } await store.send(.incrementButtonTapped) { $0.count = 1 }
  4. Root Feature (App) → Child Feature (Login) ʹ ͓͚Δςετͷ໢ཏੑͷྫ await

    store.send(\.login.submitButtonTapped) { // 1⃣ Ϣʔβʔͷ submit button λοϓΛΤϛϡϨʔτ͢Δɻ $0.login?.isLoading = true // 2⃣ login feature ͷ͢΂ͯͷঢ়ଶͷมߋΛ assertion ͢Δɻ // ... } await store.receive(\.login.loginResponse.success) { // 3⃣ login feature Ͱ͸ API ϦΫΤετ͕ߦΘΕΔͨΊɺͦͷϋϯυϦϯά͕ඞཁɻ $0.login?.isLoading = false // 4⃣ login feature ͷ͢΂ͯͷঢ়ଶͷมߋΛ assertion ͢Δɻ // ... } await store.receive(\.login.delegate.didLogin) { // 5⃣ login feature ͸ϩάΠϯ͕੒ޭͨ͜͠ͱΛ Delegate Action Λ௨ͯ͡਌ʹ఻͑Δɻ // 6⃣ app feature ͷ͢΂ͯͷঢ়ଶͷมߋΛ assertion ͢Δɻ $0.authenticatedTab = .loggedIn( Profile.State(...) ) // 7⃣ ࠷ऴతʹλϒͷঢ়ଶ͕มߋ͞ΕΔ͜ͱΛ assertion ͯ͠ςετऴྃɻ $0.selectedTab = .activity }
  5. Exhaustivity Λ o ff ʹͯ͠ Root Feature ͷ ݶΒΕͨ෦෼͚ͩͷςετΛߦ͏ store.exhaustivity

    = .off // ⬅ // Ϣʔβʔͷ submit button λοϓΛΤϛϡϨʔτ͢Δɻ await store.send(\.login.submitButtonTapped) // ࠷ऴతʹ login feature ʹΑͬͯϩάΠϯ͕੒ޭͨ͜͠ͱ͕ app feature ʹ఻ΘΓɺ // ͦΕʹΑΓى͖ΔมߋΛ assertion ͢Δ͚ͩͱͳΔɻ await store.receive(\.login.delegate.didLogin) { $0.selectedTab = .activity }
  6. Exhaustivity Λ o ff ʹ͢Δ࣌ͷςΫχοΫ • ར༻͸ Root دΓͷ Feature

    ͚ͩʹཹΊΔ • `off(showSkippedAssertions:)` • Exhaustivity Λ off ʹͭͭ͠΋ skip ͞Εͨ΋ͷΛදࣔ͢Δ • `withExhaustivity(_:operation:)` • ෦෼తʹ exhaustivity Λ off ʹͯ͠ςετͰ͖Δ • `skipReceivedActions(strict: fi le:line)` • ड͚औͬͨ action Λશͯ skip ͢Δ • `assert(_: fi le:line:)` • Store ͷঢ়ଶͷ assertion ͕ՄೳͰɺಛʹ Store ͷ࠷ऴঢ়ଶΛ assertion ͢Δͷʹศར
  7. TCA ͰςετΛॻ্͘Ͱ໾ཱͭଞͷ࿩ • `withDependencies(_:operation:)` • ෦෼తʹґଘΛॻ͖׵͑ͯςετ͢Δ • `useMainSerialExecutor` • Async

    ͳฒྻॲཧΛϝΠϯεϨουͰॲཧ͠ςελϒϧʹ͢Δ • ࠓͷ TCA ͸σϑΥϧτͰ͜ͷػೳ͕༗ޮʹͳ͍ͬͯΔ • Non-exhaustive testing ͷ஫ҙ఺ • https://pointfreeco.github.io/swift-composable-architecture/main/documentation/ composablearchitecture/testing#Non-exhaustive-testing