【问题标题】:Jasmine toThrow does not work even though an error is thrown即使抛出错误,Jasmine toThrow 也不起作用
【发布时间】:2017-04-11 23:42:04
【问题描述】:

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

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

【问题讨论】:

    标签: javascript node.js unit-testing jasmine


    【解决方案1】:

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

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

    正在尝试将调用结果实际传递给 expect(...),

    改用匿名函数:

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

    【讨论】:

      猜你喜欢
      • 2016-02-24
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 2015-01-01
      相关资源
      最近更新 更多