如何为 angular 项目编写简单的 jasmine 和 karma 测试以将对象添加到对象数组
How to write simple jasmine and karma test for angular project for addition of object to an object array
我在angular中创建了一个普通员工项目,需要测试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);
}
要测试此类功能,您应该尝试以下块:
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.
它肯定会帮助您以非常简单而标准的方式理解测试。
测试愉快!
@shashank Vivek 感谢您的回答。下面的代码在测试添加产品方法时对我有用,以测试我们是否能够将产品添加到已经存在的对象数组中。
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中创建了一个普通员工项目,需要测试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);
}
要测试此类功能,您应该尝试以下块:
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.
它肯定会帮助您以非常简单而标准的方式理解测试。
测试愉快!
@shashank Vivek 感谢您的回答。下面的代码在测试添加产品方法时对我有用,以测试我们是否能够将产品添加到已经存在的对象数组中。
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");
});