gpt4 book ai didi

javascript - ChaiJS 期望构造函数抛出错误

转载 作者:可可西里 更新时间:2023-11-01 01:22:01 25 4
gpt4 key购买 nike

我正在尝试使用 ChaiJS 作为我的断言库,使用 Teaspoon gem for Rails 测试我的构造函数是否会抛出错误。

当我运行以下测试时:

  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

最佳答案

遇到了同样的问题。用一个函数包装你的构造函数:

var fcn = function(){new badConstructor()};
expect(fcn).to.throw(Error, 'Invalid location');

关于javascript - ChaiJS 期望构造函数抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com