【问题标题】:How write unit test case for below in jasmine如何在茉莉花中编写单元测试用例
【发布时间】:2020-08-13 06:48:51
【问题描述】:
 public writeValue(item): void {
  
        if(item.length> 0)
        for(let i=0;i<=item.length-1;i++)
        {
            this.salesDivisionTagSet.push(item[i]);
        }
               

我是角度测试的新手,我无法为上述内容编写单元测试用例,因为覆盖率没有增加,有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你有没有尝试写任何东西并遇到问题?
  • 到目前为止你写了什么?如果您不熟悉单元测试用例,我建议您先学习,

标签: angular unit-testing testing jasmine


【解决方案1】:

您的测试用例如下所示。

it("should push when item when list is selected", () => {
    component.salesDivision = mockSalesDivision
    spyOn(component.salesDivisionTagSet, "push");
    component.writeValue([9,1]);
    expect(component.salesDivisionTagSet.push).toHaveBeenCalled();
})

正如您的代码所说,项目可以是数组或简单值。上面的测试用例涵盖了数组的类型。您需要编写另一个测试用例,其中值将是非数组,如下所示。

component.writeValue(78);
expect(component.salesDivisionTagSet.push).not.toHaveBeenCalled();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多