【发布时间】:2015-05-11 19:30:10
【问题描述】:
我正在尝试使用 Teaspoon gem for Rails 测试我的构造函数是否会引发错误,并使用 ChaiJS 作为我的断言库。
当我运行以下测试时:
it('does not create the seat if x < 0', function() {
var badConstructor = function() {
return new Seat({ radius: 10, x: -0.1, y: 0.2, seat_number: 20, table_number: 30});
};
expect(badConstructor).to.throw(Error, 'Invalid location');
});
我得到这个输出:
失败:
1) Seat does not create the seat if x < 0
Failure/Error: undefined is not a constructor (evaluating 'expect(badConstructor).to.throw(Error(), 'Invalid location')')
构造函数抛出错误,但我认为我没有正确编写测试。
当我尝试运行 expect(badConstructor()) 时,我得到了输出:
Failures:
1) Seat does not create the seat if x < 0
Failure/Error: Invalid location
【问题讨论】:
-
是
expect(badConstructor())吗?即,您需要调用该函数。 -
我刚刚尝试过 - 无济于事。现在我得到另一个错误输出 - 我在 OP 中添加了它。
-
所以听起来它工作正常,因为“无效位置”是错误消息的一部分。你要么需要在你的测试中选择它,要么在你的函数中添加一个
try/catch语句来返回一个你可以测试的错误。 -
但不应该指望它抛出一个错误照顾我不需要 try/catch 吗?
-
它是返回错误还是只是将某些内容记录到控制台?我敢打赌后者。
标签: javascript ruby-on-rails testing chai