概要
Quick Nimble を利用したテストコードのテンプレートです。
Quickとは
QuickはSwift、ObjcのBDD開発フレームワークです。(github)
Quick is a behavior-driven development framework for Swift and Objective-C.
- TDD: テスト駆動開発(Test Driven Development)
- BDD: ビヘイビア駆動開発(Behavior Driven Development)
準備
こちらが非常に参考になります。
http://qiita.com/akatsuki174/items/77cb95265919b5ad4965
- Cartfile.privateにQuick, Nimbleを追加
- Link Binary With LibrariesにQuick, Nimbleを追加
- Copy FilesにFrameworkでQuick, Nimbleを追加
- コード作成(TopViewController)
- テストコード作成(TopViewControllerTests)
結果
テストケース
- UILabelのTextに正しい値がセットされているか
テストコード
import UIKit
import Quick
import Nimble
@testable import basicQuickTemplate
class TopViewControllerTests: QuickSpec {
override func spec() {
describe("TopViewController") {
var topViewController: TopViewController?
beforeEach {
topViewController = TopViewController(labelText: "UILabel text matcher")
}
it("UILabel text set is correct") {
let expectedText = "UILabel text matcher"
expect(isContainUILabelWithText(topViewController!.view, text: expectedText)).to(beTrue())
}
}
}
}
private func isContainUILabelWithText(view: UIView, text: String) -> Bool {
for subview in view.subviews {
if let label = subview as? UILabel where label.text == text {
return true
}
if isContainUILabelWithText(subview, text: text) {
return true
}
}
return false
}
view.subviewsから対象textがセットされたUILableがあるか再帰的に探す