如何在CoffeeScript中使用Jasmine来测试异常?

时间:2012-08-24 01:15:58

标签: exception-handling coffeescript jasmine

我有这段代码:

class root.Goal
    constructor: (@name, @size) ->
        if @size <= 0 then throw new Error "Goal must be larger than 0 size"

通过Jasmine-CoffeeScript测试测试:

it "cannot be of size 0", ->
    expect(new p.Goal("Goal 3", 0)).toThrow "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)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

正如您已经发现的那样,您必须在匿名函数中包含对错误抛出代码的调用。如果你没有预期的调用将获得你的错误抛出代码的“结果”(如果有的话)。因此,当期望被调用时,所有“行动”已经结束。