即使抛出错误,Jasmine toThrow也不起作用

Jasmine toThrow does not work even though an error is thrown

本文关键字:Jasmine toThrow 不起作用 错误 出错      更新时间:2023-09-26

即使抛出错误,该代码也会失败。

it('should throw error when foo config value is falsey', (done) => {
  const config = { foo: null, bar: 'some-name' };
  expect(quux.withConfig(config).load('*', (err, inst) => { done(); })).toThrow();
});

我也试过这个

it('should throw error when foo config value is falsey', (done) => {
  const config = { foo: null, bar: 'some-name' };
  expect(SelfServiceCompletedJobStore.withConfig(config).load('*', (err, inst) => { })).toThrow();
  done();
});

错误信息:

should throw error when foo config value is falsey
  - Error: Requires a non-empty foo:String

您应该将一个函数传递给expect(…)。以下代码:

expect(quux.withConfig(config).load('*', (err, inst) => { done();    })).toThrow();

试图将调用的结果传递给expect(…),

使用匿名函数代替:

expect(function(){ 
    quux.withConfig(config).load('*', (err, inst) => { done(); });   
}).toThrow();