Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'.

like image 379
Aristo Avatar asked Sep 28 '16 22:09

Aristo


People also ask

How do you mock a component in Angular testing?

A mock component in Angular tests can be created by MockComponent function. The mock component respects the interface of its original component, but all its methods are dummies. To create a mock component, simply pass its class into MockComponent function.

What is the name of the test files created for components in Angular?

spec. ts file. This file contains unit tests for the main AppComponent. When running tests using the Angular CLI, all unit tests in files with the *.

Which TestBed method is used to create an Angular component under test?

Which of the following TestBed method is used to create an Angular component under test? The correct answer is - createComponent!

What is mocking in unit testing Angular?

Introduction. Mocking is a great idea for testing Angular apps because it makes maintenance easier and helps reduce future bugs. There are a few complex tools, such as XUnit, for mocking an Angular CLI project. You can execute the mocking methods described in this guide only if you use vanilla Jasmine + Angular Testbed ...


1 Answers

I finally found a solution to my problem. I used the TestBed to get the service I have injected. Then use that service to get the page Title in the current Test context. Here is my new Code

import { TestBed } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';
import { AccountComponent } from './account.component';

describe(`AccountComponent`, () => {
  let titleService: Title;
  let fixture: any;
  let component: AccountComponent;

  beforeEach(async (() => {
    TestBed.configureTestingModule({
      declarations: [AccountComponent],
      providers: [Title],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AccountComponent);

    // Access the dependency injected component instance
    component = fixture.componentInstance;
  });

  it(`should instantiate component`, () => {
    expect(component instanceof AccountComponent).toBe(true, `should create AccountComponent`);
  });

  it(`Page title should be "Account"`, () => {
    titleService = TestBed.get(Title);

    expect(titleService.getTitle()).toBe(`Account`);
  });
});
like image 78
Aristo Avatar answered Oct 21 '22 16:10

Aristo