Issue
I have a monolite app in Angular and i'm still trying to understand everything related to unit testing, i need to test some services, i often read online that when it comes to test a service i have to mock the http calls, like get, post, ecc... i can't understand why, is there a way to test it without mocking? i've also read that i could use a spy, but i don't understand how should i use it... I just need to test if some http calls returns the values that i expect, what's the most complete way i can do it? please explain it to me in the siplest way you can or paste any link related to this specific topic. thanks :)
Solution
You want to test without mocking the API, so it won't be a unit test, but rather an integration test.
Cypress If you just want a quick way to check that some http calls return the values that you expect, Cypress might be a good start. See here a tutorial on how to get started: https://circleci.com/blog/api-testing-with-cypress/. Basically, you just do a cy.request, then write your assertions about the data you expect.
Postman This might also be useful in your case: https://learning.postman.com/docs/writing-scripts/test-scripts/
Jasmine You can do real requests with Jasmine too:
beforeEach(() => {
...
TestBed.configureTestingModule({
imports: [
HttpModule // <-- simply provide the actual HttpModule here
]
});
});
it('should do real requests', async(inject([Http], (http) => {
http.get('your url').subscribe(res => {
expect(res.json()).toBe(...);
});
})));
Answered By - dopoto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.