1
使用QUinit的throw()聲明我想測試是否引發錯誤和錯誤消息。我有以下功能:正確使用QUnit throw()聲明?
/**
* 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
}
而下面QUnit
測試:
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'
)
})
然而,單元測試失敗給這個:
Error [email protected] 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)
怎麼辦?
謝謝,你是親愛的! – maasha