问题
Using QUinit's throw() assertion I want to test that an error is thrown and the error message. I have the following function:
/**
* Error function for Node.
* @param {String} msg Error message.
*/
function NodeError (msg) {
var that = this
/**
* Attribute for message.
* @type {String}
*/
this.msg = msg
/**
* Function rendering NodeError as a string.
* @return {String} String representation of NodeError.
*/
this.toString = function () {
return that.msg
}
}
/**
* Node object. TODO Fill out.
* @param {String} title Node title.
* @throws {NodeError} If no title given
*/
function Node (title) {
var that = this
if (!title) {
throw new Error('Error: no title given')
}
/**
* Node title
* @type {[type]}
*/
this.title = title
}
And the following QUnit
test:
QUnit.test('new Node w/o title throws error', function (assert) {
assert.expect(1) // Expected number of assertions
assert.throws(
function () { new Node() },
function (err) { err.toString() === 'Error: no title given' },
'Error thrown'
)
})
However, the Unit tests fails giving this:
Error thrown@ 0 ms
Expected:
function( a ){
[code]
}
Result:
Error("Error: no title given")
Diff:
function( a ){
[code]
}Error("Error: no title given")
Source:
at Object.<anonymous> (file:///Users/maasha/install/src/protwiz/test/test_pw_node.js:10:10)
What to do?
回答1:
The second function you pass to assert.throws
should return
something. You currently have a statement that evaluates to a boolean but the result is discarded. The function then returns implicitly, thus returning undefined.
In addition, you're throwing new Error(...)
, not a NodeError
. You either need to change that, or just use err.message
.
Here's a fixed version:
function NodeError (msg) {
var that = this;
this.msg = msg;
this.toString = function () {
return that.msg;
}
}
function Node (title) {
var that = this;
if (!title) {
throw new NodeError('Error: no title given'); // <- CHANGED
}
this.title = title;
}
QUnit.test('new Node w/o title throws error', function (assert) {
assert.expect(1);
assert.throws(
function () { new Node(); },
function (err) { return err.toString() === 'Error: no title given' }, // <- CHANGED
'Error thrown'
);
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.16.0/qunit.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.16.0/qunit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script>
<div id="qunit"></div>
You might want to look into using a linting tool, which might catch this problem, as well as others (e.g. you have a lot of missing semi-colons, which can lead to unexpected results).
来源:https://stackoverflow.com/questions/39515048/correct-use-of-qunit-throw-assertion