How to mock Pipe when testing Component

You can add your mockpipes in the declarations of the TestBed: TestBed.configureTestingModule({ declarations: [ AppComponent, MockPipe ], … The MockPipe needs to have the @Pipe decorator with the original name. import {Pipe, PipeTransform} from ‘@angular/core’; @Pipe({name: ‘pipename’}) class MockPipe implements PipeTransform { transform(value: number): number { //Do stuff here, if you want return value; } … Read more

Angular 2 unit testing components with routerLink

You need to configure all the routing. For testing, rather than using the RouterModule, you can use the RouterTestingModule from @angular/router/testing, where you can set up some mock routes. You will also need to import the CommonModule from @angular/common for your *ngFor. Below is a complete passing test import { Component } from ‘@angular/core’; import … Read more

Angular unit test input value

Inputs don’t have textContent, only a value. So expect(field.textContent).toBe(‘someValue’); is useless. That’s probably what’s failing. The second expectation should pass though. Here’s a complete test. @Component({ template: `<input type=”text” [(ngModel)]=”user.username”/>` }) class TestComponent { user = { username: ‘peeskillet’ }; } describe(‘component: TestComponent’, () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [FormsModule], declarations: [ TestComponent … Read more

Angular 2 Testing – Async function call – when to use

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes. If you have ever worked with Jasmine outside out Angular, … Read more

How to unit test a component that depends on parameters from ActivatedRoute?

The simplest way to do this is to just use the useValue attribute and provide an Observable of the value you want to mock. RxJS < 6 import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; … { provide: ActivatedRoute, useValue: { params: Observable.of({id: 123}) } } RxJS >= 6 import { of } from ‘rxjs’; … Read more

angular2 testing: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’

You need to import the FormsModule into the TestBed configfuration. import { FormsModule } from ‘@angular/forms’; TestBed.configureTestingModule({ imports: [ FormsModule ], declarations: [ AppComponent ], providers:[AppService] }); What you are doing with the TestBed is configuring a NgModule from scratch for the test environment. This allows you to only add what is needed for the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)