简体   繁体   English

如何在单元测试中覆盖分支

[英]How to cover branches in unit testing

I have this function which needs to be unit tested, 我有此功能需要进行单元测试,

I've wrote few specs which is working fine. 我写了一些规范,可以正常工作。 But in the code-coverage it says Else path not taken for this line if (text !== '') {} . 但是在代码覆盖率中,它表示if (text !== '') {} Else path not taken在该行Else path not taken

I'm not sure how to test variable inside the function. 我不确定如何在函数内部测试变量。

Below are few specs which I wrote. 以下是我写的一些规格。

  it('should split the content correctly', inject([GenerateInlineTagsService], (service: GenerateInlineTagsService) => {
    const obj = service.getView(RANGES);
    expect(JSON.stringify(obj)).toEqual(JSON.stringify(inlineSplitContent));
  }));

Create a new test case and pass below value to your function while testing so that else branch would be taken: 创建一个新的测试用例,并在测试时将低于值的值传递给您的函数,以便进行else分支:

const content = { inlineEntityRanges: [], text: '' }

Also, if possible write else block of each if block as while measuring branch coverage these things are required. 同样,在测量分支覆盖范围时,如果可能的else ,将每个if块写入else块,这是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM