Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using toThrowError in Jasmine

I am working on an app that makes heavy use of JavaScript. I need to unit test this code. In an effort to do that, I'm relying on Jasmine.

Some of my JavaScript code throws JavaScript Error objects. Those objects assign values to the message and name property of the Error object. I assign a type of exception to the name property. For instance, sometimes the name is set to OutOfRangeException, sometimes its ArgumentException, etc.

How do I use the toThrowError function in the Jasmine framework to test if a thrown error has a specific name? Currently, my JavaScript looks like the following:

function getRandomNumber(max) {
  if ((!isNaN(parseFloat(max)) && isFinite(max)) === false) {
    var error = new Error('You must provide a number');
    error.name = 'ArgumentException';
    throw error;
  }

  if ((max === null) || (max < 1) || (max > 100)) {
    var error = new Error('The maximum value must be greater than 0 and less than 100.');
    error.name = 'ArgumentOutOfRangeException';
    throw error;
  }

  return Math.floor(Math.random() * max) + 1;
}

function ArgumentException(message) {
  this.name = 'ArgumentException';
  this.message = message || '';
}
ArgumentException.prototype = new Error();
ArgumentException.prototype.constructor = ArgumentException;

How can I write a Jasmine test that checks for an ArgumentException error or an ArgumentOutOfRangeException error?

Thank you!

like image 327
user3192178 Avatar asked Jan 19 '14 19:01

user3192178


People also ask

What are matchers in Jasmine?

Jasmine is a testing framework, hence it always aims to compare the result of the JavaScript file or function with the expected result. Matcher works similarly in Jasmine framework. Matchers are the JavaScript function that does a Boolean comparison between an actual output and an expected output.

How do you throw an exception in Jasmine?

Let us modify our JavaScript with the following set of code. var throwMeAnError = function() { throw new Error(); }; describe("Different Methods of Expect Block", function() { var exp = 25; it ("Hey this will throw an Error ", function() { expect(throwMeAnError). toThrow(); }); });

Which of the following code of jasmine is used as a library in your project?

You can also use Jasmine as a library in your project. For example the following code imports and executes Jasmine: var Jasmine = require('jasmine'); var jasmine = new Jasmine(); jasmine. loadConfigFile('spec/support/jasmine.


1 Answers

Checking exception for a function with parameter is not supported in jasmine. But you can use below workaround to overcome this limitation and test your functions.

describe('toThrowError test case', function() {

    it('test getRandomNumber function for undefined', function() {
        expect(function() {
            getRandomNumber(undefined);
        }).toThrowError("You must provide a number");
    });

    it('test getRandomNumber function for 0', function() {
        expect(function() {
            getRandomNumber(0);
        }).toThrowError("The maximum value must be greater than 0 and less than 100.");
    });

});

toThrowError matcher takes 1 or 2 parameters

  • 1 Parameter - Either exception message or exception type
  • 2 Parameters - Exception type and Exception message

Example to check based on exception type:

function getRandomNumber(max) {
    throw new SyntaxError();
}

describe('toThrowError test case', function() {
    it('test getRandomNumber function for undefined', function() {
        expect(function() {
            getRandomNumber(undefined);
        }).toThrowError(SyntaxError);
    });
});

Refer link for different types of exceptions.

Custom Error Message

Below mentioned snippet gives a sample for using the custom error messages.

function getRandomNumber(max) {
    throw new ArgumentException();
}

function ArgumentException(message) {
  this.name = 'ArgumentException';
  this.message = message || '';
}

ArgumentException.prototype = new Error();
ArgumentException.prototype.constructor = ArgumentException;

describe('toThrowError test case', function() {
    it('test getRandomNumber function for undefined', function() {
        expect(function() {
            getRandomNumber(undefined);
        }).toThrowError(ArgumentException);
    });
});
like image 158
user3037143 Avatar answered Oct 22 '22 03:10

user3037143