gpt4 book ai didi

javascript - spy 的回调不是用 sinon 和 qunit 调用

转载 作者:行者123 更新时间:2023-11-30 17:31:39 24 4
gpt4 key购买 nike

对于单元测试,我正在尝试使用 sinon.js 制作假服务器和 qUnit , 但在调用 ajax 之后方法callbackspy没有调用。

所以第三个和第四个断言不满足,

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
});

JSFIDDLE

最佳答案

看起来像解析错误导致 ajax 错误处理程序运行而不是成功处理程序。如果您像这样修复 JSON...

    server.respondWith("GET", "/something",
[200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]']);

...测试通过。 JSFiddle .

关于javascript - spy 的回调不是用 sinon 和 qunit 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com