【问题标题】:Test if an exception was catched in Jasmine测试 Jasmine 中是否捕获到异常
【发布时间】:2016-06-16 17:46:15
【问题描述】:

如何测试Jasmine 中是否捕获到异常?

如果我删除 try...catch 语句,预期是正确的,但我对此不感兴趣。

规格

import { foo } from './src';

describe('app', () => {
  it('should match if an exception was catched', () => {
    expect(() => foo()).toThrow();
  });
});

来源

function foo() {
  try {
    throw new Error();
  } catch(e) {
    console.log(e);
  }
}

export { foo };

【问题讨论】:

    标签: javascript node.js unit-testing jasmine


    【解决方案1】:

    也许我们可以监视console.log 来检查这个输出,并测试这个Error 是否被捕获。

    // Test
    spyOn(console, 'log');
    foo();
    expect(console.log).toHaveBeenCalledWith(new Error());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2019-11-18
      • 2020-05-13
      • 1970-01-01
      • 2016-01-21
      • 2012-08-19
      • 2020-06-30
      相关资源
      最近更新 更多