Reputation: 195
I apologize if this is not an appropriate question but I am having some trouble figuring out how to implement an automated test for a save function within the app I am working on. I am unsure if I should unit test or e2e test it. The user fills out required fields in a form and clicks the save button. From there the data the user entered is sent to the server and returned to the front page of the app; the user data is displayed on this page. Any guidance in the right direction would be wonderful! Thank you. If it helps, I included the code that saves the user. Again, I am unsure if I should unit test it or e2e test it, considering I have to fill out the entire form before clicking save. Thank you once again.
saveUser() {
this.user = this.userForm.getRawValue();
var url = AppConfig.URL_AddUser;
var isToEdit = false;
this.user.UserID = this.userId;
console.log(this.user);
this.httpClient.post(url, JSON.stringify(this.user), httpOptions)
.catch((error: Response | any) => {
this.showAlertWindow(this.exceptionMessage);
console.error(error.message || error);
return Observable.throw(error.message || error);
})
.subscribe((res: any) => {
console.log(res);
if (res == "DuplicateEmailAddress")
this.showAlertWindow("This email address is already exists.");
else if (res == "success")
this.dialogRef.close({ success: "success", isToEdit: isToEdit });
});
}
Upvotes: 2
Views: 92
Reputation:
Although opinions about testing vary, in this case I'd recommend to use an e2e test. It is just a matter of time. You save time without losing test coverage. Because you cover both the logic and the presentation layer within one test.
It's true that creating e2e tests can be very time consuming, but you usually have to do them anyway and as long as you design them in a way that they call all your methods at least once, you do nothing wrong.
Upvotes: 3