摩卡,should.js和断言exception

我有一个文件app.coffee

 class TaskList class Task constructor: (@name) -> @status = 'incomplete' complete: -> if @parent? and @parent.status isnt 'completed' throw "Dependent task '#{@parent.name}' is not completed." @status = 'complete' true dependsOn: (@parent) -> @parent.child = @ @status = 'dependent' # Prepare scope stuff root = exports ? window root.TaskList = TaskList root.Task = Task 

和一个名为test/taskTest.coffee的文件:

 {TaskList, Task} = require '../app' should = require 'should' describe 'Task Instance', -> task1 = task2 = null it 'should have a name', -> something = 'asdf' something.should.equal 'asdf' task1 = new Task 'feed the cat' task1.name.should.equal 'feed the cat' it 'should be initially incomplete', -> task1.status.should.equal 'incomplete' it 'should be able to be completed', -> task1.complete().should.be.true task1.status.should.equal 'complete' it 'should be able to be dependent on another task', -> task1 = new Task 'wash dishes' task2 = new Task 'dry dishes' task2.dependsOn task1 task2.status.should.equal 'dependent' task2.parent.should.equal task1 task1.child.should.equal task2 it 'should refuse completion it is dependent on an uncompleted task', -> (-> task2.complete()).should.throw "Dependent task 'wash dishes' is not completed." 

如果我在terminal运行这个命令: mocha -r should --compilers coffee:coffee-script -R spec我有一个失败的testing(最后一个)说,它期待着一个例外“依赖任务”洗碗没有完成“。 但得到了“未定义”。

如果我改变(-> task2.complete()).should.throw to -> task2.complete().should.throw通过删除圆括号,testing通过,失败,如果我不抛出exception。 但是,如果我将exception消息更改为随机的东西,它仍然通过。 难道我做错了什么? 如果信息是字面意思的话,不应该只通过testing“依赖任务”洗碗没有完成“。

你用一个string抛出一个exception,而不是抛出一个错误对象。 throw()查找后者。 所以如果你这样做,你的原始代码工作:

 throw new Error "Dependent task '#{@parent.name}' is not completed." 

如果你在CoffeeScript中写的东西产生的结果没有意义,可以尝试编译成js(或者将代码粘贴到CoffeeScript中 ,你会看到:

 -> task2.complete().should.throw "Dependent task 'wash dishes' is not completed." 

编译为:

 (function() { return task2.complete().should["throw"]("Dependent task 'wash dishes' is not completed."); }); 

它只是定义了一个函数,并没有执行它。 这就解释了为什么改变string没有任何区别。 我希望有帮助。

首先,这是一些好看的咖啡标记。

其次,David Weldon在他的回答中是正确的,你可以改变抛出来实际抛出一个错误,它的工作原理。

这里是你的代码放在一个长文件,只是抛出改变。

 class TaskList class Task constructor: (@name) -> @status = 'incomplete' complete: -> if @parent? and @parent.status isnt 'completed' throw new Error "Dependent task '#{@parent.name}' is not completed." @status = 'complete' true dependsOn: (@parent) -> @parent.child = @ @status = 'dependent' # Prepare scope stuff root = exports ? window root.TaskList = TaskList root.Task = Task should = require 'should' describe 'Task Instance', -> task1 = task2 = null it 'should have a name', -> something = 'asdf' something.should.equal 'asdf' task1 = new Task 'feed the cat' task1.name.should.equal 'feed the cat' it 'should be initially incomplete', -> task1.status.should.equal 'incomplete' it 'should be able to be completed', -> task1.complete().should.be.true task1.status.should.equal 'complete' it 'should be able to be dependent on another task', -> task1 = new Task 'wash dishes' task2 = new Task 'dry dishes' task2.dependsOn task1 task2.status.should.equal 'dependent' task2.parent.should.equal task1 task1.child.should.equal task2 it 'should refuse completion it is dependent on an uncompleted task', -> (-> task2.complete()).should.throw "Dependent task 'wash dishes' is not completed." 

摩卡那混蛋,你很好去。