assert.alwaysThrew(spy, exception);
Like above, only required for all calls to the spy.
Passes, when the fake, spy or stub always threw the given exception.
The exception can be a String denoting its type, or an actual object.
When only one argument is provided, the assertion passes if spy always threw any exception.
js
import * as sinon from "sinon";
const f1 = sinon.fake();
f1("apple pie");
sinon.assert.alwaysThrew(f1, "TypeError");
// => Uncaught Error [AssertError]: fake did not throw exception
const f2 = sinon.fake.throws(new TypeError("not an apple pie"));
try {
f2("apple pie");
} catch (err) {
// not used
}
// Generates no error
sinon.assert.alwaysThrew(f2, "TypeError");Example using test framework
js
import tap from "tap";
import * as sinon from "sinon";
tap.test("assert.alwaysThrew - passes when all calls threw", (t) => {
const fake = sinon.fake.throws(new Error("boom"));
try {
fake();
} catch (e) {}
try {
fake();
} catch (e) {}
t.doesNotThrow(() => {
sinon.assert.alwaysThrew(fake);
}, "assertion should pass when all calls threw");
t.end();
});
tap.test("assert.alwaysThrew - fails when one call didn't throw", (t) => {
const fake = sinon.fake();
fake();
t.throws(
() => sinon.assert.alwaysThrew(fake),
/fake did not always throw exception/,
"assertion should fail when not all calls threw"
);
t.end();
});