HTTP | Angular Unit Testing Made Easy: Comprehensive Guide to HTTP Testing

HTTP | Angular Unit Testing Made Easy: Comprehensive Guide to HTTP Testing


HTTP | Angular Unit Testing Made Easy: Comprehensive Guide to HTTP Testing

HTTP | Angular Unit Testing Made Easy: Comprehensive Guide to HTTP Testing

In this video, we dive into the exciting world of Angular unit testing. Whether you’re a beginner or experienced Angular developer, understanding unit testing is crucial for building robust and reliable applications. Join us as we provide a comprehensive introduction to Angular unit testing, covering the fundamentals, best practices, and practical examples. By the end of this video, you’ll have a solid foundation to start writing effective unit tests for your Angular projects. Don’t miss out on this valuable opportunity to level up your development skills and ensure the quality of your code.

Following are the topics those will be covered as part of this Angular Unit Testing series.

How to use Spy?
How to use BeforeEach?
How to do unit testing on Services?
How to do unit testing on HTTP services?
How to do unit testing on Components?
How to do unit testing on Components having dependencies?
How to simulate DOM interaction while doing unit testing?
How to do unit testing on asynchronous operations?
How to do unit testing on promises \u0026 observables?
How to do unit testing on Pipes?
How to do unit testing on Directives?
How to do unit testing on Routing?
How to achieve code coverage?

As an angular developer, I think you may be interested in these playlists.

Advanced Angular Concepts:    • Advanced Angular Tutorial | Mastering…  

Angular Interview Concepts:    • Angular Interview Concepts | With Exp…  

Join me in this insightful video as I walk you through the process of testing HTTP services in Angular. With a focus on the crucial ‘afterEach’ block, I’ll explain its significance and demonstrate its usage within unit tests.

Discover the best practices and techniques for effectively testing HTTP services in Angular applications. From setting up the testing environment to mocking HTTP requests, you’ll gain a solid understanding of how to validate and verify your HTTP interactions.


Tags:
#angular, #tutorial, #learn, #webdevelopment, #ui, #unittesting

HTTP | Angular Unit Testing Made Easy: Comprehensive Guide to HTTP Testing


Content

0.56 -> Hi Friends
1.56 -> Welcome back to Angular Unit Testing series.
14.969 -> In this series I am talking about unit testing in Angular using Jasmine framework.
18.9 -> In the last video I have explained about how we can make use of beforeEach execution block.
24.33 -> In this video, let's see about how we can test a HTTP service.
29.52 -> I am using the same example which I have used in the last video.
33.5 -> And in that, I have added a new service called Data Service.
37.46 -> In which, I have a function called get all users, which is using http client to invoke
42.5 -> a get request to fetch some data.
45.57 -> We can also see the cli has already created the spec file in which testing module is configured
51.53 -> inside beforeEach block.
53.579 -> But if I run ng test, it will fail, because this data service is now having http client
59.14 -> as dependency.
60.36 -> But in the spec file we are not passing it as dependency and so we get the error, 'No
64.871 -> provider for HttpClient'.
67.47 -> Let's fix this.
69.5 -> The error is clear that we need to provide the HttpClient.
72.08 -> But if we provide HTTPClient, it will invoke the real http requests.
75.86 -> But as we are testing, we don't want the actual request to happen.
79.71 -> So, instead of providing HttpClient, we can import the HttpClientTestingModule.
91.13 -> And so, we can see the error is gone.
93.009 -> As a next step, we need to populate our test data, for that, we can use the HttpTestingController,
100.06 -> which we can inject here.
105.49 -> And, let's create our specification to test this getAllUsers function.
111.1 -> It should get all users.
113.689 -> Ok.
114.689 -> Inside this specification, let me invoke the getAllUsers function from the service.
118.56 -> I need to subscribe, otherwise nothing will happen.
124.25 -> And, as we are not invoking the original http request, we need to mock some data.
129.36 -> You can see, I have created a folder, inside that I have created mock data in a file.
133.89 -> I have created as a dictionary, so that I can easily get any data using the id.
139.349 -> Now, we can invoke a mock request using the expectOne function of testing controller.
145.04 -> I am passing the actual end point.
147.06 -> So that when this end point is hit, we can get our mock data.
151.53 -> For that, we need to pass our mock data to this mock request using the flush method in
155.781 -> the request.
156.781 -> I am going to use Object dot values, and so I can pass the data in an array.
162.2 -> Now, the setup is ready.
164.51 -> Let's write our expectations.
165.53 -> First, we need to make sure the data is coming.
168.94 -> For that, I can use, expect users to be truthy.
173.099 -> Next, I can validate the number of users.
175.75 -> We know, we are passing 3 users.
178.53 -> And so, I can use, expect users dot length to be 3.
185.19 -> Let me also find one particular user using the id and let me validate the content.
193.83 -> I am going to use, expect this user dot name to be this one.
203.33 -> And finally, let me also validate whether this request method is GET.
207.019 -> For that, let me use request dot request dot method to equal to GET.
213.14 -> When I save this, we can see our test is passing.
217.049 -> So, this is how we can test Http requests.
220.97 -> Let me quickly create another function in the service to find the user by id.
231.209 -> And let me copy this specification and modify as per our need.
235.769 -> Here, I can invoke the getUserById function by passing the id as one.
240.48 -> It is going to give me one user and I can expect that to be truthy and let me also validate
246.44 -> the name.
250.01 -> And here, I need to change the end point, and to the flush method I can pass this particular
254.67 -> user.
255.75 -> And so, when I save we can see test is passing.
259.44 -> To ensure, no other http request other than this one is invoked, we can use the verify
264.419 -> method from testing controller.
266.92 -> So ideally, I need to add this in both the specifications.
270.19 -> But, instead, like beforeEach, I can use afterEach and move this code inside afterEach.
280.789 -> And so, if I save, we can see, the tests are still passing.
284.82 -> Ok, we have seen how we can test the http GET methods.
289.46 -> Let's see how we can test the http PUT methods.
293.07 -> For that let me create a new function called, updateUser, in which I am updating an user
297.82 -> based on id.
300.919 -> Ok, let me write the spec for this.
305.44 -> I can copy this and modify.
310.76 -> First let me invoke the updateUser function.
313.41 -> To this we need to pass the second parameter, which is going to be the changes alone.
317.31 -> Let me change the age to 24.
320.11 -> And I can pass this changes to this function.
323.699 -> Inside this, let me also validate whether I am getting the same id.
328.06 -> And here, I need to make sure this is going to be PUT request.
331.5 -> Also, I need to pass the modified user into my flush method.
335.22 -> For that, let me get the original user and modify the age to 24.
339.79 -> I am passing this modified user into the flush method.
344.199 -> And so, I can expect request dot request dot body dot age equal to, this changes dot age.
351.75 -> Now, if I save, we can see our tests are passing.
355.95 -> Hope you understood about how to test HTTP services.
359.22 -> That's all for today.
361.039 -> Please like this video and support me.
363.32 -> I will be back with next concept soon.
365.72 -> Thank you.
366.72 -> Bye.

Source: https://www.youtube.com/watch?v=7rlwryYhGzs