如何有效地测试延迟总是

时间:2016-10-04 09:31:11

标签: javascript jquery promise qunit

在jQuery ajax(或get)always部分或甚至在bluebird promises finally中编写代码测试时,这样:

function doStuff() {
    console.log('stuff done');
}

function someFunction() {
    return $.get('someurl').always(doStuff);
}

我总是发现自己正在为此编写(QUnit)测试:

QUnit.test("doStuff will be called when someFunction succeeds", function (assert) {
    var deferred = $.Deferred();
    var backup = $.get;
    $.get = function () { return deferred; };

    var doStuffIsCalled = false;
    doStuff = function(){ doStuffIsCalled = true; };

    deferred.resolve({});
    return someFunction().then(function(){
        $.get = backup;
        assert.ok(doStuffIsCalled);
    });
});

QUnit.test("doStuff will be called when someFunction fails", function (assert) {
    var deferred = $.Deferred();
    var backup = $.get;
    $.get = function () { return deferred; };

    var doStuffIsCalled = false;
    doStuff = function(){ doStuffIsCalled = true; };

    deferred.reject(new Error('some error'));
    return someFunction().catch(function(){
        $.get = backup;
        assert.ok(doStuffIsCalled);
    });
});

这有效,但有点冗长。是否有一些更有效的方法,最好是在单个测试中,直接测试在延迟的始终部分中调用的代码?

1 个答案:

答案 0 :(得分:1)

您可以使用Sinon.js来模拟jQuery ajax(或获取)以及一般的承诺。

一种方法可能是:

function someFunction() {
    return $.get('/mytest').always(doStuff);
}

function givenFncExecutesAndServerRespondsWith(reponseNumber, contentType, response) {
    server.respondWith("GET", "/mytest", [reponseNumber, contentType, response]);
    someFunction();
    server.respond();
}

module("Testing server responses", {
    setup: function () {
        server = sinon.sandbox.useFakeServer();
        doStuff = sinon.spy();
    },
    teardown: function () {
        server.restore();
    }
});

test("doStuff will be called when someFunction succeeds", function () {
    givenFncExecutesAndServerRespondsWith(200, '', '');
    ok(doStuff.called, "spy called once");
});

test("doStuff will be called when someFunction fails", function () {
    givenFncExecutesAndServerRespondsWith(500, '', '');
    ok(doStuff.called, "spy called once");
});

您可以在此fiddle中使用此代码。如果您使用alwaysdone代替fail来调用回调,则相应的测试将失败。

对代码的解释如下:

  1. 创建一个假的服务器和一个充当always回调的间谍。
  2. 根据我们正在测试的内容修改服务器响应的响应数。
  3. 希望它有所帮助。