问题
I know how to do it in Mocha but want to know how to do it with Jasmine. I tried this
describe('test promise with jasmine', function() {
it('expects a rejected promise', function() {
var promise = getRejectedPromise();
// return expect(promise).toBe('rejected');
return expect(promise.inspect().state).toBe('rejected');
});
});
However, the state is always pending
and, of course, the test fails. I couldn't find any example online that I could make it work.
Can someone please help me with this?
Thanks.
回答1:
To test asynchronous code with jasmine you should use its async syntax, e.g.:
describe('test promise with jasmine', function(done) {
var promise = getRejectedPromise();
promise.then(function() {
// Promise is resolved
done(new Error('Promise should not be resolved'));
}, function(reason) {
// Promise is rejected
// You could check rejection reason if you want to
done(); // Success
});
});
回答2:
jasmine 2.7 onwards supports returning promises, and would have its fulfilled state tested.
To test for rejection:
it('test promise with jasmine', async () => {
try {
await getRejectedPromise();
} catch (err) {
return;
}
throw new Error('Promise should not be resolved');
});
or yet:
it('test promise with jasmine', async () => {
await getRejectedPromise()
.then(
() => Promise.reject(new Error('Promise should not be resolved')),
() => {});
});
To verify the actual message, besides the usual instanceof/toBe()
, place inside the catch
:
expect(() => { throw err }).toThrow(new MyCustomError('Custom error message'));
The benefit from this approach is to have a nicer fail message on the test output.
Expected function to throw MyCustomError: Custom error message, but it threw Another error message.
Somewhat better than the usual output.
To test for resolved (can't be simpler):
it('test promise with jasmine', async () => {
await getRejectedPromise();
});
回答3:
You can use finally
block to test promise state:
it('should resolve if auth succeed', (done)=>{
var p = server.login('user', 'password');
p.finally(()=>{
expect(p.isFulfilled()).toBeTruthy();
done();
});
});
You can use isFulfilled
to check if promise was fulfilled and value
method to check the fulfillment value. Corresponding methods for rejection are isRejected
and reason
.
回答4:
you can now use expectAsync()
Expecting success:
it('expect result', async () => {
...
await expectAsync(someAsyncFunction(goodInput)).toBeResolved(expectedResponse)
})
Expecting failure:
it('expect result', async () => {
...
await expectAsync(someAsyncFunction(badInput)).toBeRejectedWith(expectedResponse)
})
回答5:
@Leonid's answer is correct, but you can simplify like so, and use only promises:
it('test promise with jasmine', function() {
return getRejectedPromise().then(function() {
// Promise should not be resolved, so we reject it
return Promise.reject(new Error('Promise should not be resolved'));
})
.catch(function(err){
if(!/Promise should not be resolved/.test(err && err.message)){
return Promise.reject(err);
}
})
})
来源:https://stackoverflow.com/questions/27164404/test-if-a-promise-is-resolved-or-rejected-with-jasmine-in-nodejs