Spy on coffee - script constructor in silon

I am trying to verify that the coffee constructor script throws an exception (using sinon and sinon-chai).

class Animal constructor: -> throw "exception" class Dog extends Animal 

How to properly create a spy to make sure that the instance Dog command throws an exception?

+4
source share
1 answer

Just use an anonymous function to create your object inside, so you can pass it to expects . Sinona still was not.

 expect(-> new Animal()).to.throw(/message/) 

Beware that the regex in chai throw always passes if you throw a simple string. It only works correctly if you select the Error object. In any case, it is always better to throw real mistakes.

 class Animal constructor: -> throw new Error('message') # only this will assert the regexp above 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1447819/


All Articles