概要
Angularのテストして利用されるJasmineのMatchersを一覧にしてまとめていきます。
Matchersとは以下のように、期待値を比較するのに使われるメソッドです。
expect(テスト値).toBe(期待値);Matchers一覧
| Matcher | Matcherの機能概要 |
|---|---|
| not | 続くMatcherメソッドの機能を反転 |
| nothing() | なにもないこと |
| toBe(expected) | ===で一致すること |
| toBeCloseTo(expected, precision) | 指定した小数点の精度(precision)まで、数値が一致すること |
| toBeDefined() | undefined値でないこと |
| toBeUndefined() | undefined値であること |
| toBeFalse() | 完全にfalseであること |
| toBeFalsy() | falseとされる値であること |
| toBeTrue() | 完全にtrueであること |
| toBeTruthy() | trueとされる値であること |
| toBeGreaterThan(expected) | expectedよりも大きい値であること |
| toBeGreaterThanOrEqual(expected) | expected以上の値であること |
| toBeLessThan(expected) | expectedよりも小さい値であること |
| toBeLessThanOrEqual(expected) | expected以下の値であること |
| toBeInstanceOf(expected) | インスタンスのクラスがexpectedと一致すること |
| toBeNaN() | NaNであること |
| toBeNegativeInfinity() | -Infinityであること |
| toBePositiveInfinity() | Infinityであること |
| toBeNull() | nullであること |
| toContain(expected) | expectedの値を含むこと |
| toEqual(expected) | 同値一致であること |
| toHaveBeenCalled() | メソッドが呼ばれていること |
| toHaveBeenCalledBefore(expected) | expectedメソッドより先に呼ばれていること |
| toHaveBeenCalledOnceWith() | 1回だけメソッドが呼ばれていること |
| toHaveBeenCalledTimes(expected) | expectedの回数だけ、メソッドが呼ばれていること |
| toHaveBeenCalledWith() | 少なくとも一回メソッドが呼ばれていること |
| toHaveClass(expected) | expectedのクラスを持つDOM要素であること |
| toHaveSize(expected) | サイズが等しくなること |
| toHaveSpyInteractions() | スパイが呼ばれていること |
| toMatch(expected) | 正規表現(expected)に一致する値であること |
| toThrow(expected) | expectedのthrowであること |
| toThrowError(expected, message) | expectedのthrowErrorで、メッセージが一致すること |
| toThrowMatching(predicate) | throwのpredicate関数が一致すること |
| withContext(message) | コンテキストを追加する |

