2

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

IvanM
  • 2,913
  • 2
  • 30
  • 30

2 Answers2

1

You want doh.assertError()

Example:

doh.assertError(TypeError, this.field, "setValue",
    [{
        CreatedOn: "March 10th, 2014"
    }],
    "setValue() on an invalid format should throw a TypeError");
IvanM
  • 2,913
  • 2
  • 30
  • 30
Mike Wilklow
  • 108
  • 1
  • 7
0

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

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
  • I think the OP was referring more to how to have DOH recognize that an error is being correctly throw (so passing a test). Is this possible? Thanks for your help here! – streetlight Feb 26 '14 at 12:54
  • To clarify -- a passing test for an error correctly thrown in the code – streetlight Mar 06 '14 at 19:55