LoginSignup
58

More than 5 years have passed since last update.

mocha / chai チートシート

Posted at

概要

  • Mocha = jsのユニットテストツール

    • minitestなどに近いかたちでTDD/BDDが実現できる
    • Rails関連のmochaは、minitestのスタブ作成のためのツールなので別もの
  • Chai = アサーションツール

    • assert()系のメソッドを提供する
    • mochaはchaiを含む復数種のアサーションツールを選択して使える

導入

bower/npm/CDNなどからインストール可能。

bower
$ bower install mocha   #--save
$ bower install chai    #--save
npm
$ npm install mocha
$ npm install chai
CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.0.0/chai.min.js"></script>

構築

コマンドラインあるいはブラウザベースで起動できる。

ブラウザからの利用

  • テスト用の画面に、mocha.jsおよびchai.jsを読み込む。
    (環境により任意)

  • div#mochaを、結果表示用の領域として準備する。

<div id="mocha"></div>
  • mochaを実行するためのjsコードを設定する
// 1. 起動する(BDDスタイル指定)
mocha.setup('bdd');

// 2. テストケースを記述する。あるいは一旦<script>を切って外部のテストファイルを読む
// ...

// 3. テストを実行
mocha.checkLeaks();
mocha.run();
  • テストケースを実装し、画面をブラウザ表示することで、テストを実行できる。

mocha::テストケースの書き方

テストケースはBDD, TDD, EXPORTS, QUNITなどの復数のスタイルを選択して記述できる。
(mocha.setup()で指定する)

BDD

ビヘイビア(= 振る舞い = 「xxはxxなときxxする(はず)」)単位でテストを記述する。

BDDの例
// describe()でテストを階層的に分類
describe('User', function(){

    describe('attributes', function(){

        // id()でテストケースを定義
        it('has attribute name', function(){

            // 実際のアサーション処理はchaiなどを使う
            // ...
        });

    });

    describe('ajax', function(){

        it('can save', function(done){

            // 非同期処理をテストしたい場合は、it()の引数にとったdone() を呼ぶことでテスト完了を知らせる
            User.save(function(){
                done();
            }));
        });
    );

});

chai::アサーションの書き方

Assertion, BDDの2種類の書き方ができる。
いずれも検証に失敗すると例外が発生し、結果としてmochaがテストの失敗を結果表示する。

Assertion

旧来のアサート形式によって検査する。

アサーションの例

// user.attrが{}であることを確認する
chai.assert.isObject(user.attr);

// user.attr.idの値が-1であることを確認する
chai.assert.propertyVal(user.attr, 'id', -1);

アサーションのはかなり多い。
まただいたいのメソッドには、その検証項目の否定形がある。
(equal()に対するnotEqual()など)

アサーションメソッド抜粋
isOk()              // true
deepEqual()         // 同値
isAbove()           // >
isAtLeast()         // >=
isBelow()           // <
isAtMost()          // <=
isTrue()            // == true
isFalse()           // == false
isNull()            // == null
isNaN()             // isNan()
isUndefined()       // === undefined
isFunction()        // instanceof Function
isObject()          // instanceof Object
isArray()           // instanceof Array
isString()          // instanceof String
isNumber()          // instanceof Number
isBoolean()         // instanceof Boolean
typeOf()            // typeof
instanceOf()        // instancef
include()           // ex. 'foobar' includes 'foo'
lengthOf()          // .length
match()             // RE

一覧は公式のリファレンスを参照のこと。

Except/Should

メソッドチェーンにより、英文のような形でBDD的な振る舞いを記述できる方法。

Except/Shouldの例
var foo = 'bar';

// expectで書く
expect(foo).to.be.a('string');

// shouldで書く
foo.should.be.a('string');

参考

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
58