如何在Angular中测试javascript“ DELETE”运算符?

时间:2019-06-24 05:29:40

标签: angular unit-testing karma-jasmine

我需要一些帮助。我想测试for in循环,该循环删除对象属性。我的问题是我的测试未包含在代码覆盖范围内。

代码覆盖率页面显示了未经测试的操作员“删除”。所以我不知道如何测试此运算符。

功能测试很简单。我测试了对象必须看起来像这样,并且在调用此函数之后,该对象必须看起来像这样。测试成功,但没有覆盖代码。

我的功能:

export class AnimalSounds {
animals = { cat: 'Meow', dog: 'Woof', cow: 'Muuuu' }; 

  removeProperty() {
     for (const key in this.animals) {
        if (key === 'cow') {
          delete this.animals[key];
        }
     }
  }
}

我的测试:

it('should delete property cow', () => {
  const object = { cat: 'Meow', dog: 'Woof', cow: 'Muuuu' }; 
  const objectWithoutCow = { cat: 'Meow', dog: 'Woof' };

  expect(component.animals).toEqual(object);
  component.removeProperty();
  expect(component.animals).toEqual(objectWithoutCow);
});

0 个答案:

没有答案