This Question have no Text answers yet!

How to mock angular2 platform-browser Title component for testing purpose

Question!

I am writing unit tests for Angular 2 component with Jasmine. I will like to test if my document title as been set to a specific value when my component is instantiated.

Here is my component

import { Component } from [email protected]/core';
import { Title }     from [email protected]/platform-browser';

@Component({
  selector: 'cx-account',
  templateUrl: 'app/account/account.component.html',
})
export class AccountComponent {
  public constructor(private 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 [email protected]/core/testing';
import { Title, By }           from [email protected]/platform-browser';
import { AccountComponent } from './account.component';

describe('AppComponent Tests', function () {
  let titleService: Title = new Title(); 
  beforeEach(() => {
    TestBed.configureTestingModule({
        declarations: [AccountComponent],
        providers:    [ {provide: Title } ],      
    });
   let fixture = TestBed.createComponent(AccountComponent);

   });

  it('Title Should be Account', () => {
    expect(titleService.getTitle()).toBe('Account');
  });      
});

Karma output is :

Error: Expected 'Karma DEBUG RUNNER' to be 'Account'.



Video about How to mock angular2 platform-browser Title component for testing purpose