编写mocha测试用例时的时序问题

时间:2015-08-03 00:12:46

标签: javascript node.js mocha chai

我正在尝试使用mocha为我的一个REST Apis编写一个测试用例。

My Rest api看起来像这样:

server.route({
    method : "DELETE",
    path   : "/local/{id}",

    handler: function (request, reply) {
        var id = request.params.id;
        return getId(id)
        .then(function (result) {
            return testFunction(result, id, reply);
        })
        .catch (function (err) {
            reply(400).err;
        })
    }

    function testFunction(result, id, reply) {
        return update(id, result)
        .then(function (resp) {
            reply(resp);
            stopSomething(id)
            .then(function () {
                //do some work
                return Bluebird.resolve(data);
            })
            .catch(function (error) {
                //handle error & just log..user does not need to know
                //stopSomething is a promise chain which runs in background
            });
        })
        .catch(function (err) {
            //handle error and reply to user
        });
    }
});

为了测试这个,我编写了以下测试用例:

describe("with an valid id", function () {
    var stopSomethingStub;
    var result
    before(function () {
        stopSomethingStub = Sinon.stub(stopSomethinbObject, "stopSomething", function () {
            return Bluebird.resolve();
        });
        return new Request("DELETE", "/local/id123").inject(server)
        .then(function (data) {
            result = data;
        });
    });
    after(function () {
        //do clean up of stubs
    });

    it("deletes id", function () {
        expect(result.statusCode).to.equal(200);
        expect(stopSomethingStub.called).to.be.true;
        //Few more checks
    });
}

现在,"它"在收到DELETE请求的200 Ok响应后立即执行块。但是我想在检查断言之前完成整个promise链。如果我把它作为第一个期望块,则stopSOmethingStub.called显示为false。但是,如果我将它作为最后一个断言保持起作用。我认为这是一些时间问题。

0 个答案:

没有答案
相关问题