Test Automation
For tests we decided to use Karma -http://karma-runner.github.io/1.0/index.html
The Karma test runner is ideal for writing and running unit tests while developing the application. It can be an integral part of the project's development and continuous integration processes.
Next i will describe step by step how to create test in context of out project structure. We have three main file categories - Components, Services, Modules.
Testing Components:
Let's assume we have a components folder with following structure:
-app
-components
-testComponent
-test.component.ts
-test.component.html
-test.component.css/app/components/test.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
title = "Hello world!!!";
}/app/components/test.component.html
/app/components/test.component.css
So for create a test you should to do following:
1) Create file test.component.spec.ts in the root of yours components directory. You can create file with any name by "spec.ts" should be always present in it.
2) Add to /app/components/test.component.spec.ts file following content:
Testing services and models
Let's assume we have a folder with following structure:
/app/services/todo.data.service.ts
/app/models/todo.ts
Next you need to create two test files for each component with names todo.spec.ts and todo.service.test.ts with following contents:
/app/services/todo.service.test.ts
/app/models/todo.spec.ts
Next you can execute test by run "npm test" command in the root of your Angular 2 project.
You will have test execution information in terminal and in yours browser also.
Useful information:
How to test services -https://angular.io/docs/ts/latest/guide/testing.html#!#component-with-dependency
How test test Asynchronous services -https://angular.io/docs/ts/latest/guide/testing.html#!#component-with-async-service
How to test routes -https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component
Last updated