1

我有这个代码:

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)

有任何想法吗?

4

1 回答 1

1

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

于 2012-08-27T15:00:01.170 回答