如何编写简单的茉莉花和业力测试以将对象添加到对象数组的角度投影

问题描述 投票:0回答:2

我已经创建了一个正常的角度员工项目,需要测试addproduct功能。我应该如何为此编写一个测试用例。我不使用服务,这是一个简单的推送功能。任何对此的帮助将是有帮助的

public products = [{
     name:"Moto G5",
     quantity:2
  },
  {
     name:"Racold Geyser",
     quantity:3 
  }];

  addproduct(name: string,quantity:number) {

      var details = {name:name,quantity:quantity};
      this.products.push(details);

  }
javascript angular typescript jasmine karma-jasmine
2个回答
0
投票

要测试这样的功能,您应该尝试以下块:


it('should be created', () => {
  component.products.length = 2;
  component.addproduct("test",1);  
  expect(component.products.length).toBe(3);
  expect(component.products[2].name).toBe("test");
});


由于您是Angular测试的新手,因此强烈建议您阅读this article which has some more links in the last paragraph.

这肯定会帮助您以非常简单但标准的方式理解测试。

测试愉快!


0
投票

@@ shashank Vivek,谢谢您的回答。下面的代码为我测试了add product方法,以测试我们是否能够将产品添加到已经存在的对象数组中。

it('should be created', () => {
  component.products.length = 2;
  component.addproduct("test",1);  
  expect(component.products.length).toBe(3);
  expect(component.products[2].name).toBe("test");
});
© www.soinside.com 2019 - 2024. All rights reserved.