所以我试图测试一个函数,它是一个客户端函数(未完成),这就是为什么它嵌入在测试本身中的原因(直到我能找到更好的解决方案)。
我遇到的问题是当我测试该函数是否引发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");


});

最佳答案

好吧,您的第一个问题是!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)。我仍在寻找可以通过这种方式进行操作的选项,但希望这对您有所帮助。

关于javascript - 断言磁带- Node ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34142011/

10-10 00:51