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 a ArgumentOutOfRangeException error?
Thank you!
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);
});
});
If would like to pass your test as soon as your method throws an Error. Then you could use try-catch
block and when you reach the catch
block, you expect your test to be passed.
function ErrorFunction(args:any[]) {
throw new Error('An error message');
}
describe('Testing a function that throws an error',()=>{
it('should throw an Error',()=>{
try {
ErrorFunction(args)
} catch (e) {
expect(true).toBeTruthy();
}
})
})
来源:https://stackoverflow.com/questions/21221697/using-tothrowerror-in-jasmine