【发布时间】:2012-08-19 14:02:09
【问题描述】:
我有这个代码:
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)
有什么想法吗?
【问题讨论】:
-
我也这么认为...所以我尝试在匿名函数中调用“new p.Goal("Goal 3", 0),如下所示:expect(f = do -> new p.Goal("Goal 3", 0)),但这也不起作用。
-
原来这个语法有效:expect(-> new p.Goal("Goal 3", 0)).toThrow "Goal must be greater than 0 size"
-
对,
expect想要执行一个函数,但do会在调用expect之前执行该函数。
标签: exception-handling coffeescript jasmine