【发布时间】:2014-05-20 20:19:24
【问题描述】:
对于单元测试,我正在尝试使用sinon.js 和qUnit 制作假服务器,但是在调用ajax 方法后,spy 的callback 没有被调用。
这就是为什么第三个和第四个断言不满足的原因,
ok(callback.called, "spy called once"); //failed
ok(callback.calledWith([{ id: 12, comment: "Hey there" }]); //failed
谁能告诉我我做错了什么,这是我尝试过的。
test("should fetch comments from server", function () {
var server = this.sandbox.useFakeServer();
server.respondWith("GET", "/something",
[200, { "Content-Type": "application/json" },
'[{ id: 12, comment: "Hey there" }]']);
var callback = this.spy();
//----^---this is not invoking
//after invoke below $.ajax() code above callback should be invoked
//and 3rd and 4th assertion should be satisfied but not happening.
$.ajax({
url: "/something",
success: callback
});
server.respond();
equal(server.requests.length, 1, "server request length");
ok(callback.called, "spy called once"); //failed
ok(callback.calledWith([{ id: 12, comment: "Hey there" }])); //failed
});
【问题讨论】:
标签: javascript jquery unit-testing qunit sinon