テストファイルは、Sinon.JSのページを参考にしてます。
参考
/sample/jsTestDriver.conf
load:
- lib/jquery-1.8.0.js
- src/*.js
test:
- lib/sinon-1.4.2.js
- lib/jsTestDriverExt.js
- test/*.js
sinon.logを定義。
/sample/lib/jsTestDriverExt.js
sinon.log = function () {
jstestdriver.console.log.apply(jstestdriver.console, arguments);
};
/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+$/, "");
};
/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);
/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);
/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);
/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);
/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);