Reputation: 6351
For unit testing, I am trying to make fake server with sinon.js
and qUnit
, but after invoke the ajax
method the callback
of spy
is not invoking.
That's why third and fourth assertion are not satisfied,
ok(callback.called, "spy called once"); //failed
ok(callback.calledWith([{ id: 12, comment: "Hey there" }]); //failed
Can someone tell me what I am doing wrong, Here what I have tried.
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
});
Upvotes: 1
Views: 475
Reputation: 1129
Looks like a parse error is causing the ajax error handler to run instead of the success handler. If you fix up the JSON like this...
server.respondWith("GET", "/something",
[200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]']);
...the test passes. JSFiddle.
Upvotes: 2