dimanche 24 mai 2015

How to test throw new error JavaScript

I'm trying to write unit-tests. In case the function gets the negative number it throws new error.

   Obj = function () { 
}; 
Obj.prototype.Count = function (number) { 
    if (number < 0) { 
        throw new Error("There is no factorial for negative numbers"); 
    } else...

My unit-tet function:

function test(then,expected) {
        results.total++;
        var m1=new Obj();
        if (m1.Count(then)!=expected){
          results.bad++;
            alert(m1.Count(then)+" not equal "+expected);
        }
    }
    var results = {
        total: 0,
        bad: 0
    };

Then I'm trying to run the tests

test(5,120) 
test(-5, "There is no factorial for negative numbers");

The first one works correctly, but I don't know what to write to 'expected' for negative numbers. The example doesn't work. Could you advise me, please?

Thank you!

Aucun commentaire:

Enregistrer un commentaire