Reputation: 44
I have created a normal employee project in angular and need to test the addproduct
function. How should I write a test case for this. I don't use service it is a simple push functionality. Any help on this will be helpful
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);
}
Upvotes: 1
Views: 861
Reputation: 44
@shashank Vivek thanks for your answer. The below code worked for me in testing the add product method to test whether we are able to add the product to the already existing array of objects.
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");
});
Upvotes: 1
Reputation: 17504
To test such functions, you should try below block:
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");
});
Since you are new to Angular testing, I would strongly recommend you to read this article which has some more links in the last paragraph.
It would surely help you to understand testing in a very easy yet standard way.
Happy Testing !
Upvotes: 1