lundi 7 décembre 2015

Asserting throws in tape - node

So I am trying to test out a function, it is a client-side function(un-finish) which is why it is embedded in the test itself(until I can figure out a better solution).

The problem I am having is when I test to see if the function is throwing a TypeError.

I understand the problem is because it is testing the return value and not the function itself, I am unsure how to work around this.

Any and All help is appreciated!

Tape


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");


});

Aucun commentaire:

Enregistrer un commentaire