简体   繁体   English

茉莉节点Javascript承诺测试

[英]Jasmine Node javascript promise testing

I want to test some promises with jasmine node. 我想用茉莉花节点测试一些承诺。 However, the test runs but it says that there are 0 assertions. 但是,测试运行,但是它说有0个断言。 This is my code, is there something wrong? 这是我的代码,有什么问题吗? The then part is successfully called, so if I have a console.log there, it gets called. 接下来的部分被成功调用,因此如果我在那里有console.log,它将被调用。 If I have the code test a http request, on the success, the assertion is correctly interpretated. 如果我让代码测试一个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();
    });
});

}); });

You need to specify the done argument to the callback you pass to it , so Jasmine knows you are testing something asynchronously: 您需要为传递给it的回调指定done参数,因此Jasmine知道您正在异步测试某些东西:

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

When you include that parameter, Jasmine will wait for a while for you to call done so it knows when you're done. 当您包含该参数时,Jasmine将等待一段时间以供您调用done以便知道何时完成。

    done(); 

Secondly, in an asynchronous test, it probably is better to fail with a call of done.fail : 其次,在异步测试中,最好通过调用done.fail来失败:

    done.fail();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM