【问题标题】:QUnit & sinon. Spying on a function called asynchronouslyQUnit & sinon。监视异步调用的函数
【发布时间】:2016-01-02 03:54:38
【问题描述】:

使用 Sinon,我试图从我的 qunit 测试中的函数中窥探异步函数调用:

test("requestLiveCategoriesData should call parseCategoriesData", function(){
        var spy = sinon.spy(this.liveCategoriesModel, 'parseCategoriesData');
        this.liveCategoriesModel.requestLiveCategoriesData();
        sinon.assert.calledOnce(spy);
    });

尽管 parseCategoriesData 确实被 requestLiveCategoriesData 调用,但测试失败 (expected parseCategoriesData to be called once but was called 0 times) - 我知道这一点,因为当我在浏览器中运行测试时,parseCategoriesData called 会输出到控制台

这是我正在测试的代码(为了问题而简化):

requestLiveCategoriesData: function () {
    console.log('getting live categories');
    try {
        console.log("--- RETRIEVING LIVE CATEGORIES EVENTS ---");

        liveCategoriesCall = new LiveEventRequest(eventObjRequest);
        liveCategoriesCall.on('reset', this.parseCategoriesData, this); //the spied on function is called from here
        liveCategoriesCall.fetch({ 
            success: function (collection, resp, options) { 
                console.log('Live Categories Events Request complete.');
            },
            error: function(collection, resp) {
                console.log("Error on Live Categories Events Request");
                if (_.has(resp, 'statusText') && resp.statusText === "timeout") {
                    /* Timeout error handling */
                    console.log("Live Categories Events Request Timeout");
                }
                Conf.generalNetworkError();
            },
            complete: function (resp, textStatus) {
                console.log("Live Categories Request teardown.");
                if (liveCategoriesCall) { liveCategoriesCall.off('reset', that.parseCategoriesData, that); }
            },
            cache:false,
            timeout: that.get('liveEventsTimeout')
        });

    } catch(err) {
        console.log("ERROR: PROCESSING LIVE CATEGORIES");
        console.log(err.message);
        console.log(err.stack);
        if (liveCategoriesCall) { liveCategoriesCall.off('reset', this.parseEventsData, this); }
        this.set({ 
            'lastRequest': (new Date()).getTime(),
            'liveCategories': []
        });
        this.trigger("errorAPI", err.message);
    }
},    


parseCategoriesData: function (liveCategoriesCall) {
    console.log('parseCategoriesData called');
},

我这样做的方法是否正确?

【问题讨论】:

    标签: backbone.js asynchronous qunit sinon


    【解决方案1】:

    您至少需要使用async() 指示QUnit 等待异步响应调用。

    现在,当您完成设置后,您需要确定何时可以致电sinon.assert.calledOnce(spy);。目前似乎无法知道LiveEventRequest 何时返回数据。

    如果您无法使用 setTimeout 修改当前代码,则等待是您唯一(错误)的选择。

    如果您可以更改您的代码,您可能应该调查一下您是否可以从requestLiveCategoriesData 调用中返回一个承诺。当数据到达时,让 promise 得到解决。然后,您可以在执行 Sinon 检查之前等待该承诺,然后按照 QUnit 异步文档中的 done() 调用进行。

    当我们这样做时:您可能应该使用 sinon fakeserver 或其他方式来模拟 LiveEventRequest 的结果。

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2013-06-29
      • 2013-01-25
      • 2013-08-15
      相关资源
      最近更新 更多