角度单元测试 keyCode?

时间:2021-04-09 14:19:46

标签: angular typescript jasmine karma-jasmine

我正在开发一个 Angular 应用程序。 我想增加我的项目的测试覆盖率。 对于这种情况,如何在 angular 中实现单元测试? [46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1

1 个答案:

答案 0 :(得分:0)

indexOf(e.keyCode) !== -1 表示您正在检查该数组中是否存在该 keyCode 。您可以这样做:

it('should run the test'. () => {
  component.keyCode = 2 // Give a value which is not present in that array.
  spyOn(component, 'yourMethodWhereThatCodeIsThere').and.callThrough();
  component.yourMethodWhereThatCodeIsThere();
  expect(component.yourMethodWhereThatCodeIsThere).toHaveBeenCalled();
})

This should cover that line