mercredi 28 septembre 2016

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

I am writing unit tests for angular2 component with jasmine. I will like to test if my document title as been set to a specific value when my component is instanciate.

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(private titleService: Title ) {
    titleService.setTitle("Account");
  }
}

Here what I have wrote for testing but it is not working. titleService.getTitle() gives me Karma debug runner page title.

import { TestBed }      from '@angular/core/testing';
import { Title, By }           from '@angular/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'.

Aucun commentaire:

Enregistrer un commentaire