Questions tagged [angular2-testing]

289 questions
14
votes
1 answer

Error: Unexpected value 'FormGroup' declared by the module 'DynamicTestModule'

Our project structure follows this: Angular2-webpack-starter. Our project successfully compiles, build and can be seen in the browser. No problems here. But when we try to run the test cases using karma and jasmine we are getting this…
14
votes
2 answers

spyOn could not find an object to spy upon for start()

I am using angular-cli testing framework. inside my component , I have used 'ng2-slim-loading-bar' node module. submit(){ this._slimLoadingBarService.start(() => { }); //method operations } Now when I am testing this component, I have…
Bhushan Gadekar
  • 13,485
  • 21
  • 82
  • 131
14
votes
1 answer

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 {…
Aristo
  • 313
  • 2
  • 12
14
votes
2 answers

Angular2 RC5 Mock Activated Route Params

I need to be able to mock the activated route parameters to be able to test my component. Here's my best attempt so far, but it doesn't work. { provide: ActivatedRoute, useValue: { params: [ { 'id': 1 } ] } }, The ActivatedRoute is used in the…
GlacialFlames
  • 352
  • 1
  • 2
  • 11
13
votes
3 answers

Unit Testing angular2 component with imported module

I am trying to write a test on a component which uses angular-material2, but when I add it to my testModule declarations I get: Error: Template parse errors: 'md-card-title' is not a known element: 1. If 'md-card-title' is an Angular…
13
votes
2 answers

NG2 RC5: HTTP_PROVIDERS is deprecated

So, in version RC5 of Angular2, they deprecated the HTTP_PROVIDERS and introduced the HttpModule. For my application code, this is working fine, but I'm struggling to make the change in my Jasmine tests. Here's what I'm currently doing in my specs,…
hartpdx
  • 2,230
  • 2
  • 15
  • 18
12
votes
2 answers

Angular 2 Http testing multiple connections

Can anyone help me with testing Http requests in Angular 2. I have a service that gets a stream from two http requests. How do I mock this behaviour in my test? loadData() { return Observable.forkJoin( …
Jusef
  • 239
  • 3
  • 11
11
votes
3 answers

Angular2 Component: Testing form input value change

I have a text input and i'm listening for the changes. mycomponent.ts ngOnInit() { this.searchInput = new Control(); this.searchInput.valueChanges .distinctUntilChanged() .subscribe(newValue =>…
10
votes
1 answer

Write angular2 tests and changing the mock return values - make it DRY?

I'm writing some tests for a service and I'm altering the response from mock functions to test various cases. At the moment, every time I want to change the response of a mock, I need to reset the TestBed and configure the testing module again,…
dafyddPrys
  • 898
  • 12
  • 23
10
votes
4 answers

How to mock NavParams in tests?

This might be an Ionic 2 only question, as I don't see NavParams in the Angular 2 docs, but some concepts might translate so I tagged both. Given that I call navparams.get('somekey') in order to listen to parameters that are passed in, it's tricky…
Ka Mok
  • 1,937
  • 3
  • 22
  • 47
10
votes
2 answers

testing router-outlet component in angular2

i have a homecomponent where the html is //home.component.html //home.component.ts import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { HomeService} from…
mounika20
  • 213
  • 2
  • 9
10
votes
1 answer

Unit testing in angular2, dependency injection

Starting out with angular 2 after spending time with angular 1. Not having unit tested this much as it's more of a side project thing, I'm trying at least start out OK... I started with the example from AngularClass if that makes a…
Jorg
  • 7,219
  • 3
  • 44
  • 65
10
votes
1 answer

How to do a unit test for Http post,put,delete using MockBackend in Angular2?

So I am testing my angular2 components & services. So far I have used mockBackend for mocking get request in service as below: /* tslint:disable:no-unused-variable */ import { MockBackend } from '@angular/http/testing'; import { Http,…
Bhushan Gadekar
  • 13,485
  • 21
  • 82
  • 131
9
votes
1 answer

Angular 2 -Unit Test: How to expect throw new Error("error") from component

I writing unit test for angular 2 by jasmine + karma. I have the component check exception and throw error. And write unit test to check this case. But I seen an error message when trigger fixture.detectChanges(); 'Unhandled Promise…
ThuyNguyen
  • 1,127
  • 3
  • 14
  • 24
9
votes
4 answers

How to replace a component used in @viewChildren for a test double?

Suppose I have a component I want to test that uses a very complex component. Furthermore it calls some of its methods using references obtained by @viewChildren. For example @Component({ moduleId: module.id, selector: 'test', …
user2832323
  • 163
  • 2
  • 7
1 2
3
19 20