How to check if function throw an error in DOH?

376 Views Asked by At

I'm trying to check if a function throws error, and made this:

 define([
     'doh/runner',
     'app/Obj'
 ], function(
     doh,    
     Obj 
 ){
     doh.register('Test Obj exception', [
         function () {
             try {          
                 new Obj(); // should throw error
             } catch(e) {
                 doh.t(e, 'should give an error if no parameters given');               
             }
         }
 ]);

Obj.js file:

...
constructor: function (args){
  if (!args) { throw 'Error' }
  ...
}
...

But maybe where is some right method for this thing in Doh ? Can someone explain? Thanks

2

There are 2 best solutions below

0
Mike Wilklow On BEST ANSWER

You want doh.assertError()

Example:

doh.assertError(TypeError, this.field, "setValue",
    [{
        CreatedOn: "March 10th, 2014"
    }],
    "setValue() on an invalid format should throw a TypeError");
2
Paul Grime On

This example test shows that DOH catches and displays an error correctly.

This gist is the test, and contains this code:

var Obj = function () {
    if (arguments.length < 1) {
        throw 'Error - There are ' + arguments.length + ' arguments';
    }
};
define(["doh/runner"], function(doh){
    var tests = [
        function () {
            new Obj(); // wrong call
        }
    ];
    doh.register('Test Obj exception', tests);
});

The screenshot shows the 1 error, and the error msg from the Error thrown:

enter image description here