【发布时间】:2020-04-18 14:03:00
【问题描述】:
我正在尝试使用我的 chai 测试来捕获构造函数中抛出的错误:
'use strict'
const chai = require('chai')
const expect = chai.expect
describe('A rover must be placed inside the platform', function() {
it('A Rover at position -1 2 S should throw an error', function() {
expect(new Rover(-1, 2, 'N')).should.throw(Error ('Rover has landed outside of the platform'));
})
})
class Rover {
constructor(x, y, heading) {
this.x = x;
this.y = y;
this.heading = heading;
if (this.x > 5 || this.x < 0 || this.y > 5 || this.y < 0) {
throw Error(`Rover has landed outside of the platform`);
}
}
}
构造函数正确抛出错误,但测试没有捕获它:
A rover must be placed inside the platform
1) A Rover at position -1 2 S should throw an error
1 failing
1) A rover must be placed inside the platform
A Rover at position -1 2 S should throw an error:
Error: Rover has landed outside of the platform
是否甚至可以用 chai 捕获构造函数中抛出的错误?
【问题讨论】:
-
您执行此操作的方式与测试引发错误的 任何事物 相同 - 延迟执行。请参阅文档和例如stackoverflow.com/questions/36216868/…
标签: javascript chai