【问题标题】:The callback of spy is not invoking with sinon and qunitspy 的回调不是用 sinon 和 qunit 调用的
【发布时间】:2014-05-20 20:19:24
【问题描述】:

对于单元测试,我正在尝试使用sinon.jsqUnit 制作假服务器,但是在调用ajax 方法后,spycallback 没有被调用。

这就是为什么第三个和第四个断言不满足的原因,

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

【问题讨论】:

    标签: javascript jquery unit-testing qunit sinon


    【解决方案1】:

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

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

    ...测试通过。 JSFiddle.

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2017-02-12
      相关资源
      最近更新 更多