问题
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.
回答1:
In the case above Mocha chains returned promise after $rootScope.$apply()
was called, so chained then
needs another $rootScope.$apply()
to be executed. Without this the rest of promise chain is not executed and results in timeout.
Returning promises in Mocha specs is intended for asynchronous specs, this is necessary for testing non-Angular promises. $q
promises are synchronous and tied to Angular digests.
As shown here, chai-as-promised
can be modified to support $q
promises and apply $rootScope.$apply()
automatically to asserted promises:
chaiAsPromised.transferPromiseness = function (assertion, promise) {
assertion.then = promise.then.bind(promise);
if (!('$$state' in promise))
return;
inject(function ($rootScope) {
if (!$rootScope.$$phase)
$rootScope.$digest();
});
};
来源:https://stackoverflow.com/questions/37974675/do-i-really-need-to-return-a-promise-in-test-when-using-chai-as-promised