I am writing unit tests for Angular 2 component with Jasmine. I would like to test if my document title has been set to a specific value when my component is instantiated.
Here is my component
import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: `cx-account`,
templateUrl: `app/account/account.component.html`,
})
export class AccountComponent {
public constructor(titleService: Title) {
titleService.setTitle(`Account`);
}
}
Here what I have written for testing, but it is not working. titleService.getTitle()
gives me Karma debug runner page title.
import { TestBed } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';
import { AccountComponent } from './account.component';
describe(`AppComponent`, () => {
const titleService: Title = new Title();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AccountComponent],
providers: [{ provide: Title }],
});
});
it(`Title should be "Account"`, () => {
expect(titleService.getTitle()).toBe(`Account`);
});
});
Karma output is :
Error: Expected 'Karma DEBUG RUNNER' to be 'Account'.
providers: [{ provide: Title, useClass: Title }]
. Just regularproviders: [Title]
is totally enough. – Nerval