Questions tagged [angular2-testing]

289 questions
131
votes
2 answers

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

I am trying to test angular2 two-way binding for control input. Here is the error: Can't bind to 'ngModel' since it isn't a known property of 'input'. The app.component.html
beewest
  • 4,486
  • 7
  • 36
  • 63
125
votes
13 answers

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

I am unit testing a component that is used to edit an object. The object has an unique id that is used in order to grab the specific object from an array of objects that are hosted in a service. The specific idis procured through a parameter that is…
Colby Cox
  • 1,639
  • 2
  • 10
  • 16
96
votes
2 answers

Angular 2 Testing - Async function call - when to use

When do you use the async function in the TestBed when testing in Angular 2? When do you use this? beforeEach(() => { TestBed.configureTestingModule({ declarations: [MyModule], schemas: [NO_ERRORS_SCHEMA], …
xiotee
  • 1,519
  • 2
  • 14
  • 23
75
votes
4 answers

Angular unit test input value

I have been reading official Angular2 documentation for unit testing (https://angular.io/docs/ts/latest/guide/testing.html) but I am struggling with setting a component's input field value so that its reflected in the component property (bound via…
Zyga
  • 2,367
  • 3
  • 22
  • 32
70
votes
3 answers

Angular 2 unit testing components with routerLink

I am trying to test my component with angular 2 final, but I get an error because the component uses the routerLink directive. I get the following error: Can't bind to 'routerLink' since it isn't a known property of 'a'. This is the relevant code…
select
  • 2,513
  • 2
  • 25
  • 36
67
votes
4 answers

Testing error case with observables in services

Let's say I have a component that subscribes to a service function: export class Component { ... ngOnInit() { this.service.doStuff().subscribe( (data: IData) => { doThings(data); }, …
66
votes
9 answers

How to mock Pipe when testing Component

Currently I am overriding providers to use mocked services like this: beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { tcb.overrideProviders(AddFieldToObjectDropdownComponent, [ provide(ServiceA, {…
harunurhan
  • 1,516
  • 1
  • 17
  • 21
50
votes
6 answers

How to test Angular2's router.navigate?

I've run into missing messages in other unit tests, but just to have a nice isolated example, I created an AuthGuard that checks if a user is logged in for certain actions. This is the code: canActivate(route: ActivatedRouteSnapshot,…
Bart
  • 769
  • 1
  • 5
  • 9
49
votes
1 answer

Cannot read property 'subscribe' of undefined after running npm test (Angular 2 unit testing)

I've created a testing (spec) file for a component I'm testing. But when I run the test, it gives me an error saying Cannot read property 'subscribe' of undefined TypeError: Cannot read property 'subscribe' of undefined at…
Aiguo
  • 3,416
  • 7
  • 27
  • 52
42
votes
4 answers

Testing ngOnChanges lifecycle hook in Angular 2

Given the following code I try to test the ngOnChanges lifecycle hook of Angular2: import { it, inject, fdescribe, beforeEachProviders, } from '@angular/core/testing'; import {TestComponentBuilder} from…
user1448982
  • 1,200
  • 3
  • 12
  • 22
41
votes
3 answers

Test pipe with dependencies on services

I have a pipe that sanatises HTML as below: import { Pipe, PipeTransform } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; @Pipe({ name: 'sanitiseHtml' }) export class SanitiseHtmlPipe implements PipeTransform…
34
votes
2 answers

What is the difference between fakeAsync and async in Angular testing?

I know that tick() function utilizes fakeAsync(). And also I can use fixture.whenStable().then() with async() and fakeAsync() as well. I want to know the exact use case for both of them. Can anyone explain this with examples. Note : I want to use…
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
32
votes
2 answers

Unhandled Promise rejection: Cannot match any routes

When I run this unit test: it('can click profile link in template', () => { const landingPageLinkDe = linkDes[0]; const profileLinkDe = linkDes[1]; const aboutLinkDe = linkDes[2]; const findLinkDe = linkDes[3]; const addLinkDe =…
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
31
votes
1 answer

Is there typescript definition for DOM nativeElement?

I am testing an Angular2 component and want to assert nativeElement's property of the component, but there is no typescript definition for it. My test looks like this: beforeEach( () => { myComponentFixture =…
Dainius Lukša
  • 966
  • 1
  • 11
  • 22
31
votes
3 answers

Angular 2 Final Release Router Unit Test

How do I unit test routers in Angular version 2.0.0 with karma and jasmine? Here's what my old unit test looks like in version 2.0.0-beta.14 import { it, inject, injectAsync, beforeEach, beforeEachProviders, …
xphong
  • 1,114
  • 2
  • 13
  • 20
1
2 3
19 20