node.jsmocha.jsshould.js

Issue catching errors using should.js/Mocha


I am currently using Mocha and assertion library should.js

I am trying to run through a situation in my unit tests where it will throw an exception - but from looking at the documentation I haven't had much luck in getting it to work thus far.

The following block of code is currently what am working with:

it('Adds a new employee to the db - FAILS', funct
     let employeeObj = {                          
         "Title": "Mr",                           
         "FirstName": "Keanu ",                   
         "LastName": "Reeves",                    
         "Username": "KeanuReeves2",              
         "Password": "Password",                  
         "Email": "keanu@reeves.com", 
         "IsActive": true           
     };                                           
     should(function () {                         
         db.AddNewEmployee(employeeObj);          
     }).throw("U wot m8");                        
     done();                                      
 });            

And I keep getting the error:

AssertionError: expected Function { name: '' } to throw exception
    at Assertion.fail (node_modules\should\cjs\should.js:275:17)
    at Assertion.value (node_modules\should\cjs\should.js:356:19)
    at Context.<anonymous> (common\spec\knexDBServiceSpec.js:213:25)

Has anyone had this issue or be able to give me some guidance as to where am going wrong?


Solution

  • Its doing exactly what you set it up to do.

    You told should, that your function should throw an exception "U wot m8". But it didnt, thus the should failed.

    The expectation (the should) was that it should throw.