2016-04-15 18 views
0

我試圖檢查拒絕承諾的錯誤消息,如果我在測試中使消息字符串錯誤,它仍然通過。如何使用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. 

回答

0

你就忘記了,最終,您的電話之前,回報!

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

我試過了。仍然通過,如果我有字符串'沒有attr foo defined2' – chovy

+0

而且在通話之前的回報呢! –

+0

如果我添加返回它只是超時,說'done()'沒有被調用。 – chovy