正确使用 QUnit throw() 断言? [英] Correct use of QUnit throw() assertion?

查看:62
本文介绍了正确使用 QUnit throw() 断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 QUinit 的 throw() 断言我想测试是否抛出错误以及错误消息.我有以下功能:

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
}

以及下面的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'
  )
})

但是,单元测试失败了:

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)

怎么办?

推荐答案

你传递给 assert.throws 的第二个函数应该 return 一些东西.您当前有一个评估为布尔值的语句,但结果被丢弃.然后函数隐式返回,从而返回undefined.

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.

此外,您抛出的是 new Error(...),而不是 NodeError.您要么需要更改它,要么只使用 err.message.

In addition, you're throwing new Error(...), not a NodeError. You either need to change that, or just use err.message.

这是一个固定版本:

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>

您可能想考虑使用 linting 工具,它可能会发现这个问题,以及其他问题(例如,您有 lot 缺少分号,这可能导致 意外结果).

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).

这篇关于正确使用 QUnit throw() 断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆