CocoaPodsを導入
$ gem install cocoapods
$ pod setup
テストターゲットの追加
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単位で実行できないのがいまいち)
非同期テスト
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
サンプルコード