Jasmine 节点 javascript 承诺测试

Jasmine Node javascript promise testing

我想用 jasmine 节点测试一些 promise。但是,测试运行但它表示有 0 个断言。这是我的代码,有什么问题吗? then 部分被成功调用,所以如果我在那里有一个 console.log ,它就会被调用。如果我让代码测试一个 http 请求,成功时,断言被正确解释。

describe('Unit tests', function () {
it("contains spec with an expectation", function() {
    service.getAllClients().then(function (res) {
        expect("hello world").toEqual("hello world");
        done();
    }).catch(function (err) {
        fail();
    });
});

});

您需要为传递给 it 的回调指定 done 参数,以便 Jasmine 知道您正在异步测试某些内容:

it("contains spec with an expectation", function(done) {
    // ...

当您包含该参数时,Jasmine 会等待一段时间让您调用 done,以便它知道您何时完成。

    done(); 

其次,在异步测试中,最好通过调用 done.fail:

来失败
    done.fail();