LoginSignup
12
12

More than 5 years have passed since last update.

Xcode5環境でKiwiを導入する

Last updated at Posted at 2014-04-01

CocoaPodsを導入

$ gem install cocoapods
$ pod setup

テストターゲットの追加

プロジェクトにテストターゲットがあればそのまま、
イメージ001.jpg

なければ、左下の+を押して追加する。
イメージ002.jpg

Podfileの作成

プロジェクト直下にPodfileを作成する。

Podfile
# Podfile
platform :ios, '7.0'
target :KiwiSampleTests, :exclusive => true do
    pod 'Kiwi/XCTest'
end

kiwiのインストール

Xcodeを終了してから、

$ pod install
Analyzing dependencies
Downloading dependencies
Installing Kiwi (2.2.4)
Generating Pods project
Integrating client project

[!] From now on use `KiwiSample.xcworkspace`.

プロジェクト名.xcworkspaceが作成されるので、そちらを開く。

テスト作成

KiwiSampleTestフォルダ以下に、SampleSpec.mというファイルを作成する。

SampleSpec.m
#import "Kiwi.h"

SPEC_BEGIN(MathSpec)

describe(@"Math", ^{
    it(@"is pretty cool", ^{
        NSUInteger a = 16;
        NSUInteger b = 26;
        [[theValue(a + b) should] equal:theValue(42)];
    });
});

SPEC_END

cmd+Uでテストが実行される。

ソースコードの左側のボタン的なものを押すと、SPEC_BEGIN単位で実行できる。
(itやdescribe単位で実行できないのがいまいち)
イメージ003.jpg

非同期テスト

SPEC_BEGIN(AsyncTest)
context(@"Fetching service data", ^{
    it(@"should receive data within one second", ^{
        NSURL *url = [NSURL URLWithString:@"http://mixi.jp"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        __block NSString *fetchedData = nil;
        NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    return;
                                                }
                                                fetchedData = [[NSString alloc] initWithData:data encoding:NSJapaneseEUCStringEncoding];  //mixiはEUC-JP
                                            }];
        [task resume];

        [[expectFutureValue(fetchedData) shouldEventually] beNonNil];  //ここで最長1秒待ってから評価する(デフォルト)
    });  
});

SPEC_END

サンプルコード

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