Exception handling 如何让咖啡脚本中的茉莉花测试异常?

Exception handling 如何让咖啡脚本中的茉莉花测试异常?,exception-handling,coffeescript,jasmine,Exception Handling,Coffeescript,Jasmine,我有以下代码: class root.Goal constructor: (@name, @size) -> if @size <= 0 then throw new Error "Goal must be larger than 0 size" 异常似乎被抛出,但未被测试处理: cannot be of size 0 Failures: 1) cannot be of size 0 Message: Error: Goal must b

我有以下代码:

class root.Goal
    constructor: (@name, @size) ->
        if @size <= 0 then throw new Error "Goal must be larger than 0 size"
异常似乎被抛出,但未被测试处理:

cannot be of size 0
Failures:
  1) cannot be of size 0
   Message:
     Error: Goal must be larger than 0 size
   Stacktrace:
     Error: Goal must be larger than 0 size
    at new Goal (/var/lib/stickshift/1d4f33cd01e442eaa154aed2e7697ca7/app-root/data/235917/prioritization/process.coffee:14:15)

有什么想法吗?

因为您已经发现,您必须将对错误的调用包装到一个匿名函数中。如果您不这样做,那么调用expect将得到抛出错误代码的“结果”(如果有)。因此,当expects被调用时,所有的“操作”都已经结束。

因为您已经发现,您必须将调用包装为在匿名函数中抛出代码的错误。如果您不这样做,那么调用expect将得到抛出错误代码的“结果”(如果有)。因此,当expects被调用时,所有的“操作”都已经结束。

可能重复的操作我也这么认为……因此我尝试在匿名函数中调用“new p.Goal”(“Goal 3”,0),如下所示:expect(f=do->new p.Goal”(“Goal 3,0)),但这也不起作用。结果表明此语法起作用:expect(->new p.Goal”(“Goal 3,0)).toThrow”目标必须大于0大小“是的,
expect
想要执行一个函数,但是
do
会在调用
expect
之前执行该函数。可能是重复的,我也这么认为……所以我尝试在匿名函数中调用“new p.Goal”(“Goal 3”,0),如下所示:expect(f=do->new p.Goal(“Goal 3”,0)),但是这也不起作用。结果证明这个语法起作用了:expect(->new p.Goal(“Goal 3”,0))。toThrow“Goal必须大于0 size”对,
expect
想要执行一个函数,但是
do
会在调用
expect
之前执行该函数。
cannot be of size 0
Failures:
  1) cannot be of size 0
   Message:
     Error: Goal must be larger than 0 size
   Stacktrace:
     Error: Goal must be larger than 0 size
    at new Goal (/var/lib/stickshift/1d4f33cd01e442eaa154aed2e7697ca7/app-root/data/235917/prioritization/process.coffee:14:15)