Testing Angular components
We have just seen how to test a service in Angular application. Now, let's discuss testing an Angular component. Perform the following steps to create AppComponent
for the application:
- Create a file named
app.component.ts
.
- Import modules such as
Component
,TodoService
, andTodo
that are necessary for theAppComponent
, as shown:
import { Component } from '@angular/core'; import { Todo } from './todo'; import { TodoService } from './todo.service';
- Define the
AppComponent
class, as demonstrated:
export class AppComponent {}
- Decorate the
AppComponent
class by the@Component
attribute with theselector
,providers
andtemplateUrl
metadata:
@Component({ selector: 'my-app', templateUrl: './app.component.html', providers: [TodoService] }) export class AppComponent { }
- Declare the
todos
,todoService
,newTodoText
, andtitle
variables:
todos: Array<Todo>...