0
5

More than 1 year has passed since last update.

XCTContext.runActivityを使ってユニットテストをわかりやすくする

Posted at

XCTContext.runActivityとは

公式のリンクはこちらです。
https://developer.apple.com/documentation/xctest/xctcontext/2923506-runactivity/

Creates and runs an activity with the provided block of code.
とのことで直訳すると
提供されたコードのブロックを使ってアクティビティを作成及び実行します
みたいな意味です。

簡単にいうと、これを使うとテストが構造化できて何をしているのか名前もつけることができるということです。

runActivity自体はXcode9から追加されたものでそれまではSpec記法が使えるQuickを導入していることも多かったと思います。

実際に使ってみる

UnitTests.swift
        XCTContext.runActivity(named: "searchButtonDidPush") { _ in
            XCTContext.runActivity(named: "when before called") { _ in
                XCTContext.runActivity(named: "`fetchRepositories` is not called") { _ in
                    XCTAssertEqual(callCount, 0)
                }
            }

            XCTContext.runActivity(named: "when after called") { _ in
                XCTContext.runActivity(named: "`fetchRepositories` is called") { _ in
                    presenter.searchButtonDidPush(searchText: "Swift")
                    XCTAssertEqual(callCoount, 1)
                }
         }

テスト結果も構造化された状態を見ることができるので、とても便利です。

0
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
5