【问题标题】:How to write unit test case in jasmine?如何在 jasmine 中编写单元测试用例?
【发布时间】:2021-07-11 10:27:36
【问题描述】:
Flag(a) {
           let element=this.selected.filter(item => item.a=== a)
           return element.length > 1 ? true : false;
        }

不确定如何为上述函数编写单元测试用例。你能帮忙吗

【问题讨论】:

标签: angular jasmine karma-jasmine karma-coverage


【解决方案1】:

一个测试用例总是包含 3 个步骤:

  • 准备工作
  • 执行
  • 验证

在你的情况下,这意味着:

  • 准备:设置this.selected的值
  • 执行:使用定义的 a 调用 Flag
  • 验证:检查函数是否返回错误

【讨论】:

    【解决方案2】:

    这应该很容易:

    it('should run #Flag(a) method', () => {
      component.selected = [{ someKey1: 'someValue1' }, { someKey2: 'someValue2' }];
      const val = 'someValue1';
      spyOn(component, 'Flag').and.callThrough();
      component.Flag(val);
      expect(component.Flag).toHaveBeenCalled();
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2023-03-06
      • 1970-01-01
      • 2021-09-02
      • 2017-05-29
      • 2023-03-24
      • 2015-11-10
      相关资源
      最近更新 更多