JsTestDriver / Sinon.JSを用いてたテストについて

テストファイルについて

テストファイルは、Sinon.JSのページを参考にしてます。

参考

Sinon.JS - Versatile standalone test spies, stubs and mocks for JavaScript
本サイトのサンプルを使用
Test-Driven JavaScript Development
Sinon.JSの作者が書いた書籍。
テスト駆動JavaScript
上記の書籍の日本語版

jsTestDriver関連ファイル

jsTestDriver設定ファイル

/sample/jsTestDriver.conf

load:
 - lib/jquery-1.8.0.js
 - src/*.js
test:
 - lib/sinon-1.4.2.js
 - lib/jsTestDriverExt.js
 - test/*.js

jsTestDriver拡張ファイル

sinon.logを定義。

/sample/lib/jsTestDriverExt.js

sinon.log = function () {
    jstestdriver.console.log.apply(jstestdriver.console, arguments);
};

ライブラリファイル

jQueryライブラリ

/sample/lib/jquery-1.8.0.js

Sinon.JSライブラリ

/sample/lib/sinon-1.4.2.js

ソースファイル(テスト対象)

/sample/src/server.js

function getTodos(listId, callback) {
    jQuery.ajax({
        url: "/todo/" + listId + "/items",
        success: function (data) {
            // Node-style CPS: callback(err, data)
            callback(null, data);
        }
    });
}

/sample/src/trim.js

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/, "");
};

テストファイル

Fake server/Fake XMLHttpRequestを用いたテスト

/sample/test/fakeServerXhr_test.js

var sinonTestCase = sinon.testCase({
    setUp:function () {
        this.server = this.server.create();
    },
    "test server":function () {
        var callback = this.spy();
        getTodos(42, callback);

        // This is part of the FakeXMLHttpRequest API
        this.server.requests[0].respond(
            200,
            { "Content-Type":"application/json" },
            JSON.stringify([
                { id:1, text:"Provide examples", done:true }
            ])
        );

        assert(callback.calledOnce);
    },
    "test server request":function () {
        this.stub(jQuery, "ajax");
        getTodos(42, this.spy());

        assertTrue(jQuery.ajax.calledWithMatch({ url:"/todo/42/items" }));
    },
    "test server requests":function () {
        var callback = this.spy();
        getTodos(42, callback);
        // This is part of the FakeXMLHttpRequest API
        this.server.requests[0].respond(
            200,
            { "Content-Type":"application/json" },
            JSON.stringify([
                { id:1, text:"Provide examples", done:true }
            ])
        );

        assertTrue(this.server.requests.length === 1);
        assertEquals(this.server.requests[0].url, "/todo/42/items");
        assertEquals(this.server.requests[0].responseText, '[{"id":1,"text":"Provide examples","done":true}]');
    }
});

TestCase("Fake server xhr test", sinonTestCase);

Fake timersを用いたテスト

/sample/test/fakeTimers_test.js

var sinonTestCase = sinon.testCase({
    setUp:function () {
    },
    tearDown:function () {
    },
    "test clock":function () {
        var spy = this.spy();
        setTimeout(function () {
            spy();
        }, 10);
        this.clock.tick(9);
        assertTrue(spy.notCalled);
        this.clock.tick(1);
        assertTrue(spy.calledOnce);
    }
});

TestCase("Fake timers test", sinonTestCase);

Mocksを用いたテスト

/sample/test/mock_test.js

var sinonTestCase = sinon.testCase({
    setUp:function () {
    },
    tearDown:function () {
    },
    "test mock trim":function () {
        var mock = this.mock(String.prototype);
        mock.expects("trim").once().returns("a string");

        assertTrue("a string" === "".trim());
        mock.verify();
    }
});

TestCase("Mock test", sinonTestCase);

Spiesを用いたテスト

/sample/test/spy_test.js

var sinonTestCase = sinon.testCase({
    setUp:function () {
    },
    tearDown:function () {
    },
    "test spy trim":function () {
        this.spy(String.prototype, "trim");

        assertTrue("a string" === "   a string".trim());
        assertTrue(String.prototype.trim.calledOnce);
    }
});

TestCase("Spy/Stub test", sinonTestCase);

Stubsを用いたテスト

/sample/test/stub_test.js

var sinonTestCase = sinon.testCase({
    setUp:function () {
    },
    tearDown:function () {
    },
    "test stub trim":function () {
        this.stub(String.prototype, "trim").returns("a string");

        assertTrue("a string" === "".trim());
        assertTrue(String.prototype.trim.calledOnce);
    }
});

TestCase("Spy/Stub test", sinonTestCase);