如何使用chai-as-promised测试被拒绝的承诺和错误消息?

时间:2016-04-15 20:18:01

标签: angularjs karma-runner chai chai-as-promised

我正在尝试检查被拒绝的承诺上的错误消息,如果我在测试中使消息字符串错误,它仍会通过。

MyService.doSomething({ id: 12345 }).should.eventually.be.rejectedWith(TypeError, 'no attr foo defined2');



  svc.doSomething = function(thing){
    var def = $q.defer();

    if ( !thing.foo ) {
        def.reject(new Error('no attr foo defined'));
    } else {
        def.resolve(thing);
    }

    return def.promise;
  };

更新,似乎我的测试无法正常工作。当我退货时超时:

  it.only('should error', function(){
    // todo this should fail but it just times out
    return MyService.doSomething({ id: 12345 }).should.eventually.be.rejectedWith(TypeError, 'no attr foo defined2');
  });







  svc.doSomething = function(thing){
    var def = $q.defer();

    if ( !thing.foo ) {
        def.reject(new Error('no attr foo defined'));
    } else {
        def.resolve(thing);
    }

    return def.promise;
  };

这是错误:

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

1 个答案:

答案 0 :(得分:0)

您在通话前忘记了最终和返回。!

return MyService.doSomething({ id: 12345 }).should.eventually.be.rejectedWith...