Unit Testing Angular Application using Jasmine

Unit Testing Angular Application using Jasmine

Unit testing is one of the most important part of any application development. It helps us to make our source code more robust. Purpose of this article is to cover Jasmine test cases for core component of Angular Application.

Prerequisites:

  1. You have created your project with Angular-CLI. i.e. All dependencies are installed and Karma config is ready.

Step 1: Create Global Mocks

Source Code of Global Mocks

Step 2: Test cases for Angular Component

It will be having basic test case of checking Component is created or not.
All services injected into Component should be mocked once. We need to integrate those mocked services into this component’s specs.

Our Angular Component is as below:

Source Code of Angular Component

Spec file for our Angular Component is as below:

Spec File for Angular Component

Step 3: Test cases for Service

Similar to Step 2, when new service is created using Angular-CLI, it will create spec file with very basic test case. We need to integrate global mocks into this service’s specs.

Our Angular Service is as below:

Source Code of Angular Service

Spec file for our Angular Service is as below:

Spec File for Angular Service

Conclusion:

  • Global Mocks
  • Angular Component Test Cases
  • Angular Service Test Cases

Hope this blog post will help you in getting up the pace for Unit testing Angular application using Jasmine

Programmer who loves coding and enjoys learning new technologies. 8 years of experience in JavaScript development. Website: http://www.yuvrajpatil.com