如何在茉莉花和茉莉花中测试诺言因果报应

时间:2015-07-02 06:36:36

标签: angularjs unit-testing tdd jasmine karma-jasmine

我有一个测试用例如下:

it("Should have a valid structure", function(done){ 

    var data;
    module(APP_MODULE_NAME);

    inject(function(_metaService_){
        metaService = _metaService_;            
    });

    metaService.fetchEntityMeta('person').then(function(data){
        expect(data.status).toBe( true );
        done();
    });

});

但是我收到了以下错误:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我增加了jasmine.DEFAULT_TIMEOUT_INTERVAL但没有使用。 但是下面的代码确实有效。

    setTimeout(function(){
        expect(true).toBe(true);
        done();
    }, 5000);

所以,我的理解是我有承诺的问题。 我的问题是如何检查承诺返回的数据。

1 个答案:

答案 0 :(得分:0)

您没有在测试中的任何位置调用done()回调。将最后一个语句更改为

metaService.fetchEntityMeta('person').then(function(data) {
    expect(data.status).toBe(true);
    done();
});

另一种选择是

metaService.fetchEntityMeta('person').then(function(data) {
    expect(data.status).toBe(true);
}).then(done);