Reputation: 100
expect(fn).toThrow()
in protractor.The function (fn)
I'm passing returns a promise (protractor.promise.defer
) which protractor supposedly forces expect()
to deal with properly.
browser.wait()
's timeout parameter)deferred.reject()
which throws a catchable error
.thenCatch()
instead of .then()
on a promising functiondeferred.fulfill()
and throwing an error in the function "manually"
throw new Error();
throw {name: 'Error', message: 'timed out'}
throw {name: 'Exception'}
.fulfill()
and .reject()
, meaning the only way for the expect to move on is for the error to get thrown.return
statements, so that the expect
won't finish the function unless it throws the error..toThrowError()
instead of .toThrow()
.toThrow()
via custom matchers, but I have no idea how to make the anonymous function in .thenCatch()
make the original matcher return its result
object without returning a promise.Everytime the expect fails and the error is still thrown and uncaught (exactly as the syntax says it will be thrown).
expect
properly for this case?throw
happens in an anonymous function passed to a .then(function(){ /*error thrown here*/ })
and not the original fn
function?.toThrow()
method to account for the possibility.Upvotes: 4
Views: 1739
Reputation: 802
Chai as Promised might help to have a cleaner syntax, e.g. something like:
expect(fn_returning_promise()).to.eventually.be.rejectedWith(...)
Upvotes: 1
Reputation: 4040
Assuming your promise will be fulfilled/rejected later in time (async), you can't rely on the expect(function() {..}.toThrow()
to work in that case.
What I would do is something like this (not used to protractor promises):
it('.....', function(done) {
MyPromise(...)
.then(... my normal process which should throw....)
.then(function() {
// Error not thrown by the process, so fail the test.
expect(true).toBe(false);
},
function(err) {
// Expected error thrown so pass the test.
done();
});
});
Assuming this is how you catch errors with protractor promises ?
Some other implementation have a .catch()
method you can chain.
Upvotes: 1