LoginSignup
3
3

More than 5 years have passed since last update.

【Swift】Quick Nimble のテンプレート(テストコード)- UILabel

Posted at

概要

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があるか再帰的に探す

3
3
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
3
3