JavaScript Asserting that a function throws exceptions with Qunit assert that method throws exception java,assert that method throws

I am new to Qunit and unit testing.

I am trying to figure out what and how to test the following function. It does not do much at the moment but I wanted to assert that if I pass it incorrect values that errors are being thrown:

function attrToggle (panel, attr) {
    'use strict';

    if (!panel) { throw new Error('Panel is not defined'); }
    if (!attr) { throw new Error('Attr is not defined'); }
    if (typeof panel !== 'string') { throw new Error('Panel is not a string'); }
    if (typeof attr !== 'string') { throw new Error('Attr is not a string'); }
    if (arguments.length !== 2) { throw new Error('There should be only two arguments passed to this function')}

};

How do I go about asserting that an error will be thrown if any of these conditions are not met?

I tried to look at Qunit's 'raises' assertion but think I am misunderstanding it. My interpretation would be that the test passes if an error is thrown.

So if I tested something like this:

test("a test", function () {
    raises(function () {
        throw attrToggle([], []);
    }, attrToggle, "must throw error to pass");
});

The test should pass because errors are thrown.

Answer:1

A couple of things wrong, a working example is at http://jsfiddle.net/Z8QxA/1/

The main issue is that you are passing the wrong thing as the second argument to raises(). The second argument is used to verify that the correct Error has been thrown, so it either expects a regex, a constructor of the type of error, or a callback that allows you do your own verification.

So in your example, you were passing attrToggle as the type of error that would be thrown. Your code actually throws an Error type and so the check actually failed. Passing Error as the second argument works as you want:

test("a test", function () {
    raises(function () {
        attrToggle([], []);
    }, Error, "Must throw error to pass.");
});

Secondly, you don't need the throw keyword when calling attrToggle() inside raises().

Answer:2

The usual way of defining an isolated resource in AngularJS is: angular.service('TheService', function($resource){ return $resource('api/url'); }); I'm trying to figure out the best way to write a ...

The usual way of defining an isolated resource in AngularJS is: angular.service('TheService', function($resource){ return $resource('api/url'); }); I'm trying to figure out the best way to write a ...

I have a web application that has a dynamic javascript calendar, which allows users to set events for a given data and time. I need to push notifications to the users, so I need to convert the date ...

I have a web application that has a dynamic javascript calendar, which allows users to set events for a given data and time. I need to push notifications to the users, so I need to convert the date ...

  1. convert datetime to user timezone apex
  2. convert datetime to user timezone

What is the best way to highlight a specific substring in a div element in an event? By highlight, I mean apply some CSS style, like yellow background or something. Basically I need a simple JS ...

What is the best way to highlight a specific substring in a div element in an event? By highlight, I mean apply some CSS style, like yellow background or something. Basically I need a simple JS ...

I need height on the div 50px in default and it has to be changed to 300px onmouseover. I coded in below manner to implement it. <style type="text/css"> #div1{ height:50px; overflow:hidden; } #...

I need height on the div 50px in default and it has to be changed to 300px onmouseover. I coded in below manner to implement it. <style type="text/css"> #div1{ height:50px; overflow:hidden; } #...

  1. expand div height onmouseover