Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I replace Jasmine's toThrow matcher with the following, which lets you match on the exception's name property or its message property. For me this makes tests easier to write and less brittle, as I can do the following:</p> <pre><code>throw { name: "NoActionProvided", message: "Please specify an 'action' property when configuring the action map." } </code></pre> <p>and then test with the following:</p> <pre><code>expect (function () { .. do something }).toThrow ("NoActionProvided"); </code></pre> <p>This lets me tweak the exception message later without breaking tests, when the important thing is that it threw the expected type of exception.</p> <p>This is the replacement for toThrow that allows this:</p> <pre><code>jasmine.Matchers.prototype.toThrow = function(expected) { var result = false; var exception; if (typeof this.actual != 'function') { throw new Error('Actual is not a function'); } try { this.actual(); } catch (e) { exception = e; } if (exception) { result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected) || this.env.equals_(exception.name, expected)); } var not = this.isNot ? "not " : ""; this.message = function() { if (exception &amp;&amp; (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { return ["Expected function " + not + "to throw", expected ? expected.name || expected.message || expected : " an exception", ", but it threw", exception.name || exception.message || exception].join(' '); } else { return "Expected function to throw an exception."; } }; return result; }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload