Skip to content

Instantly share code, notes, and snippets.

@naokits
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naokits/9341781 to your computer and use it in GitHub Desktop.
Save naokits/9341781 to your computer and use it in GitHub Desktop.

Expecta

マッチャーフレームワーク for Objective-C/Cocoa

ExpectaはGithubのメンバーが作ったマッチャーです。BDDフレームワークであるSpecta(これもGithub製)と組み合わせて使用すると、手軽にテストコードが書けます。特に非同期テストの書きやすさは秀逸です。是非皆さんも一度使ってみることをお勧めします。

このドキュメントは私がspecta/expectaから、自分が必要とする部分のみを抜粋して翻訳したものです。 翻訳に自信がない部分はそのままにしています。

セットアップ

CocoaPodsを使用します。

target :MyApp do
  # your app dependencies
end

target :MyAppTests do
  pod 'Expecta',     '~> 0.2.3'   # expecta matchers
  # pod 'Specta',      '~> 0.1.11'  # specta bdd framework
end

使用準備

// #define EXP_OLD_SYNTAX // enable backward-compatibility
#define EXP_SHORTHAND
#import "Expecta.h"

もしEXP_SHORTHANDが定義されていない場合は、expectの代わりにEXP_expectで記述する必要があります。

Expectaはフレームワークに依存しません。 SpectaGHUnitGTMUnitなどのOCUnit(SenTestingKit)やOCUnitコンパチブルなテストフレームワークと組み合わせて使うことができます。

組込済みのマッチャー

記述 説明
expect(x).to.equal(y); オブジェクトまたはプリミティブxyを比較し、それらが同一(==)または同等(isEqual:)であれば合格
expect(x).to.beIdenticalTo(y); オブジェクトxyを比較し、それらが同一で、同一のメモリアドレスを持っていれば合格
expect(x).to.beNil(); xnilなら合格
expect(x).to.beTruthy(); xがtrue(non-zero)と評価されれば合格
expect(x).to.beFalsy(); xがfalse(zero)と評価されれば合格
expect(x).to.contain(y); NSArrayまたはNSStringのインスタンスxyを含むなら合格
expect(x).to.beSupersetOf(y); NSArray、NSSet、NSDictionary または NSOrderedSetのインスタンスxyの全ての要素を含むなら合格
expect(x).to.haveCountOf(y); NSArray、NSSet、NSDictionaryまたはNSStringxyの数または長さであれば合格
expect(x).to.beEmpty(); NSArray、NSSet、NSDictionaryまたはNSStringxが0の数または長さであれば合格
expect(x).to.beInstanceOf([Foo class]); xがクラスFooのインスタンスであれば合格
expect(x).to.beKindOf([Foo class]); xがクラスfooのインスタンスである、またはxがFooクラスを継承するクラスのインスタンスであれば合格
expect([Foo class]).to.beSubclassOf([Bar class]); クラスFooがクラスBarのサブクラスであるか、クラスBarと同一であれば合格(クラスクラスタのためにbeKindOf()を使用します)
expect(x).to.beLessThan(y); xyより小さければ合格
expect(x).to.beLessThanOrEqualTo(y); xy以下であれば合格
expect(x).to.beGreaterThan(y); xyよりも大きければ合格
expect(x).to.beGreaterThanOrEqualTo(y); xy以上であれば合格
expect(x).to.beInTheRangeOf(y,z); xyzの範囲内であれば合格
expect(x).to.beCloseTo(y); xyに近ければ合格
expect(x).to.beCloseToWithin(y, z); xz内のyに近ければ合格
expect(^{ code }).to.raise(@"ExceptionName"); コードのブロック内でExceptionNameという名前の例外が発生すれば合格
expect(^{ code }).to.raiseAny(); コードのブロック内で任意の例外が発生すれば合格
expect(x).to.conformTo(y); xがプロトコルyに準拠していれば合格
expect(x).to.respondTo(y); xがセレクタyに応答すれば合格
expect( code ).to.notify(@"NotificationName"); コードのブロックがNotificationNameというNSNotificationを生成すれば合格
expect( code ).to.notify(notification); コードのブロックが渡されたnotificationの通知と等しいNSNotificationを生成すれば合格
expect(x).to.beginWith(y); NSString、NSArrayまたはNSOrderedSetのインスタンスxyで始まれば合格(Also aliased by startWith
expect(x).to.endWith(y); NSString、NSArrayまたはNSOrderedSetのインスタンスxyで終われば合格

マッチャーの反転

Every matcher's criteria can be inverted by prepending .notTo or .toNot: 全てのマッチャーは.notTo.toNot を付けることで反転できます。

expect(x).notTo.equal(y); compares objects or primitives x and y and passes if they are not equivalent.

非同期テスト

全てのマッチャーは .will or .willNotを前に付加することによって、非同期のテストを実行することが出来ます。

expect(x).will.beNil(); passes if x becomes nil before the timeout.

expect(x).willNot.beNil(); passes if x becomes non-nil before the timeout.

デフォルトのタイムアウトは1.0秒です。この設定は [Expecta setAsynchronousTestTimeout:x] を呼び出すことで変更できます。 xは希望するタイムアウトの時間です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment