断言抛出磁带 - 节点

时间:2015-12-07 19:55:08

标签: javascript node.js testing assert node.js-tape

所以我试图测试一个函数,它是一个客户端函数(un-finish),这就是它嵌入测试本身的原因(直到我能找到更好的解决方案)。

我遇到的问题是当我测试函数是否抛出TypeError时。

我理解问题是因为它正在测试返回值而不是函数本身,我不确定如何解决这个问题。

欢迎任何和所有帮助!

磁带

test.js

var test  = require('tape');


test('GenerateRandomNumber Tests', function(assert){  

  /**
   * Generates a random number between the min/max
   * @param {int} the min value 
   * @param {int} the max value 
   * @param {array} list of values already stored
   * @return {mixed} int if success, false if exception thrown
  **/
  var GenerateRandomNumber = function( min, max, tickets ){

    try{

      if(!tickets instanceof Array){
        throw new TypeError();
      }

      min = (min) || 0;
      max = (max) || 200;

      var n = 0;

      n = ~~(Math.random() * (max - min) + min); 

      if(tickets.indexOf(n) === 1){
        GenerateRandomNumber(min, max);
      }

      return n;

    }catch(e){ return false; } 

  };

  assert.plan(4);


  var t1 = GenerateRandomNumber(0, 300, null);
  assert.equal(typeof t1, "boolean", "Should return a boolean - false");

  var t2 = GenerateRandomNumber(0, 300, [0,1,2,3,4]);
  assert.equal(typeof t2, "number", "Should return a typeof number");

  // HELP
  assert.throws(GenerateRandomNumber(0, 300, null), TypeError, "Should throw typeError");

  var t4 = GenerateRandomNumber(null, null, [0,1,2,3,4]);
  assert.equal(typeof t4, "number", "Should return a typeof number");


});

1 个答案:

答案 0 :(得分:6)

你首先问题是!tickets instanceof Array不是你想要的逻辑。这样做首先在tickets上执行not操作,然后测试它是否为instanceof Array。所以你真正想要的是:

if(!(tickets instanceof Array)){
   throw new TypeError();
}

下一个问题是,就像你说的那样,你得到GenerateRandomNumber的返回值,如果抛出错误,则false不是TypeError。如果你想保留你的try / catch并在GenerateRandomNumber中返回false,那么你不需要进行throws测试,而是需要:

assert.equal(GenerateRandomNumber(0, 300, null), false, "Should catch the TypeError and return false)

如果您想使用assert.throws,则需要从GenerateRandomNumber删除try / catch,而是执行以下操作:

var test  = require('tape');


test('GenerateRandomNumber Tests', function(assert){  

  /**
   * Generates a random number between the min/max
   * @param {int} the min value 
   * @param {int} the max value 
   * @param {array} list of values already stored
   * @return {mixed} int if success, false if exception thrown
  **/
  var GenerateRandomNumber = function( min, max, tickets ){
      if(!(tickets instanceof Array)){
        throw new TypeError('error');
      }

      min = (min) || 0;
      max = (max) || 200;

      var n = 0;

      n = ~~(Math.random() * (max - min) + min); 

      if(tickets.indexOf(n) === 1){
        GenerateRandomNumber(min, max);
      }

      return n;
  };

  assert.plan(3);

  var t2 = GenerateRandomNumber(0, 300, [0,1,2,3,4]);
  assert.equal(typeof t2, "number", "Should return a typeof number");

  // You must wrap GenerateRandomNumber from within another function 
  //so that the error being thrown doesn't cause the program to exit
  assert.throws(() => GenerateRandomNumber(0, 300, null), /error/, "Should throw typeError");

  var t4 = GenerateRandomNumber(null, null, [0,1,2,3,4]);
  assert.equal(typeof t4, "number", "Should return a typeof number");


});

我使用该选项传递一个RegExp,因为在按预期传递函数时,磁带的错误匹配非常奇怪(参见this issue)。我正在考虑这样做的选择,但希望这对你现在有所帮助。