Chai as Promised documentation states as follows:
Notice: either return or notify(done) must be used with promise assertions.
And the examples on the site are as follows:
return doSomethingAsync().should.eventually.equal("foo");
doSomethingAsync().should.eventually.equal("foo").notify(done);
The thing is; I actually wrote a test using chai as promised without returning the promise. Like so:
it('should resolve user', function () {
$state.get(state).resolve.user(dataservice, {
userId: testUser.id
}).should.eventually.eq(testUser);
$rootScope.$apply();
});
And it works perfectly fine. I am sure it does as I change testUser to something else the test fails. Just like I expected. So I am not sure if I am doing something wrong here.
In fact, when I modified the code to return a promise, it failed with error "Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test." The modified code is below:
it('should resolve user', function () {
var promise = $state.get(state).resolve.user(dataservice, {
userId: testUser.id
}).should.eventually.eq(testUser);
$rootScope.$apply();
return promise;
});
A little confused here. It might have something to do with Angular $q. To make it clear, the function resolve.user returns a $q promise.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…