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)
}
}
テスト結果も構造化された状態を見ることができるので、とても便利です。