In this Angular blog, we will learn how to build a simple Angular app and then learn about the unit testing process step by step with examples.
What is Unit Testing?
Unit Testing is the practice of testing small lonely pieces of code to ensure it is bug-free. However, it is also called as Isolated testing. If our test code utilizes any external resource, like the database, network, etc., then it is not a unit test.
Why do we use unit testing in Angular?
Unit tests are written using Jasmine (Jasmine is a behavior-driven development framework for testing JavaScript code.) and it is run to see if individual parts of an application are working accurately or not. Unit tests will either pass or fail as a result depending on if the code is working accurately or has any kind of bug in the tested part of an application. For the project's unit tests, angular use Karma as the test runner.
Why is unit testing important?
Ensures that all code meets quality standards prior to unit testing. This quality is uppermost. This ensures a reliable engineering environment. Unit testing helps with time saving, money saving, and better coding efficiency for developers.
How do you write an Angular test?
This default test script, is always added. Let’s see the app.component.spec.ts file:
Nothing has changed yet Let’s run our first test :
How to perform a unit test in Angular?
The Angular testing package includes mainly two utilities which are called TestBed and async. TestBed is the main Angular utility package.
The describe container contains various blocks (xit, it, beforeEach, etc.). Before any other block beforeEach runs first.
The first block is the beforeEach inside the container. This block runs before any other block of the app.component.spec.ts file. The app module in app.module.ts file is declared in the beforeEach block. The main component beforeEach is what we want to have in this testing environment.
To compile our component’s resources like the template, styles etc the compileComponents object is called.
Now the component has been declared in the beforeEach block. let’s check if the component is created or not.
The third block shows how we can have access to the properties of the created component. The default property title is. We can check title is change or not from the instance of the component created:
The fourth block shows how the test behaves in the browser environment. Once the component is created, it is called an example of the component created for the emulation running in the browser environment. Now that the component has been rendered, we can have access to its child element by accessing the nativeElelment object of the rendered component:
let’s test our Angular example application.
How to test an Angular service?
Services in Angular injects into the constructor. Services often depend on other service. Many times, it’s easy to create and inject these dependencies by adding providedIn: root to the injectable object which makes it accessible by any component or service:
Ways to test the QuoteService class:
In the first block, an instance of QuoteService is created.
The post model QuoteModel(text, date) is created into an array. we can check it by checking length of array. The length of the quoteList is expected to be 1:
The second block creates a post in an array.
Searching for Reliable AngularJS Development Services ? Your Search ends here.
How to test an Angular component?
In our example of Angular unit testing, the service is injected into the QuoteComponent to access its properties, which will be needed by the view:
The first two blocks in the describe container run consecutively. In the first block, the form module is imported into the configuration test. This is used ngModel as a form-related directive.
Also, QuotesComponent is also declared in configTestMod in the same as components are declared in ngModule of the appModule file. The second block creates a QuoteComponent and its instance, which is used by the other blocks:
If the instance of the component is created then this block tests :
The manipulation of all operations are injected service handles. The quoteService variable holds the injected service (QuoteService). Here, the component is yet to be rendered until the detectChanges method is called:
Now we test successfully create a post. The nativeElement object gives access to the HTML element.
In addition to accessing HTML content, you can also access an element through its CSS property. The button is disabled when the QuoteTextModel is empty or empty:
The way we access an element with its CSS properties, we can also access an element by its class name. The button clicks are fired by calling the triggerEventHandler . The click event type is specified. A quote displayed deleted from the quoteList when clicked on:
Conclusion
Unit tests ensure that each angular component or angular service of our angular application works as expected. In this blog, we demonstrated how to write unit tests, how to perform a unit test in Angular, and how to test an Angular service as well as an Angular component.
No comments:
Post a Comment