【发布时间】:2016-07-13 00:40:35
【问题描述】:
我正在使用Chai.should,我需要测试异常,但无论我尝试什么,我都无法让它工作。 docs 只解释expect :(
我有这个单例类,如果你尝试它会抛出错误
new MySingleton();
这里是抛出错误的构造函数
constructor(enforcer) {
if(enforcer !== singletonEnforcer) throw 'Cannot construct singleton';
...
现在我想检查一下是否发生了这种情况
it('should not be possible to create a new instance', () => {
(function () {
new MySingleton();
})().should.throw(Error, /Cannot construct singleton/);
});
或
new MySingleton().should.throw(Error('Cannot construct singleton');
这些都不起作用。这是怎么做的?有什么建议吗?
【问题讨论】:
-
这个问题不是重复的。它询问
subject.should.throw风格。另一个问题是expect风格。
标签: javascript unit-testing ecmascript-6 chai