Monday, May 20, 2024
108
rated 0 times [  109] [ 1]  / answers: 1 / hits: 33051  / 11 Years ago, fri, september 20, 2013, 12:00:00

Trying to test some code that throws an exception with Mocha/Chai, but having no luck, here's the simple code I'm trying to test:



class window.VisualizationsManager

test: ->
throw(new Error 'Oh no')


Here is my test:



describe 'VisualizationsManager', ->

it 'does not permit the construction of new instances', ->

manager = new window.VisualizationsManager

chai.expect(manager.test()).to.throw('Oh no')


However, when I run the spec, the test fails and throws the exception.



Failure/Error: Oh no


what am I doing wrong here?


More From » coffeescript

 Answers
1

It's probably because you are executing the function right away, so the test framework cannot handle the error.



Try something like:



chai.expect(manager.test.bind(manager)).to.throw('Oh no')


If you know that you aren't using the this keyword inside the function I guess you could also just pass manager.test without binding it.



Also, your test name doesn't reflect what the code does. If it doesn't permet the construction of new instances, manager = new window.VisualizationsManager should fail.


[#75553] Thursday, September 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;