Tho*_*kke 16 javascript testing ruby-on-rails chai
我试图测试我的构造函数将使用Teaspoon gem为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');
});
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
失败:
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')')
Run Code Online (Sandbox Code Playgroud)
构造函数抛出错误,但我认为我没有正确编写测试.
当我尝试运行expect(badConstructor())然后我得到输出:
Failures:
1) Seat does not create the seat if x < 0
Failure/Error: Invalid location
Run Code Online (Sandbox Code Playgroud)
小智 30
有同样的问题.用函数包装你的构造函数:
var fcn = function(){new badConstructor()};
expect(fcn).to.throw(Error, 'Invalid location');
Run Code Online (Sandbox Code Playgroud)
要测试构造函数内的抛出消息错误,可以使用 mocha 和 chai 编写此测试(使用 ES6 语法):
\n\n\'use strict\';\n// ES6 class definition\nclass A {\n constructor(msg) {\n if(!msg) throw new Error(\'Give me the message\');\n this.message = msg;\n }\n}\n\n// test.js\ndescribe(\'A constructor\', function() {\n it(\'Should throw an error with "Give me the message" text if msg is null\', function() {\n (() => new A()).should.throw(Error, /Give me the message/);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅此笔,检查CodePen上的 Diego A. Zapata H\xc3\xa4ntsch ( @diegoazh )对上述代码zJppaj的实时运行情况。\n\n\n
| 归档时间: |
|
| 查看次数: |
4018 次 |
| 最近记录: |