angular2-testing

Testing error case with observables in services

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 07:28:10
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); }, (error: Error) => console.error(error) ); }; }; The subscribe call takes two anonymous functions as parameters, I've managed to set up a working unit test for the data function but Karma won't accept coverage for the error one. I've tried spying on the console.error function, throwing an error and then expecting the spy to have been called but that doesn't quite do it. My unit test: spyOn(console,'error').and

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

半城伤御伤魂 提交于 2019-11-27 05:07:01
问题 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 id is procured through a parameter that is passed via routing, specifically through the ActivatedRoute class. The constructor is as follows: constructor(private _router:Router, private _curRoute:ActivatedRoute, private _session:Session) { } ngOnInit() { this._curRoute.params.subscribe

Angular 2 Testing - Async function call - when to use

醉酒当歌 提交于 2019-11-27 04:09:16
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], }); }); And when do you use this? beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [MyModule], schemas: [NO_ERRORS_SCHEMA], }); })); Can anyone enlighten me on this ? Paul Samsotha 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

What is the difference between fakeAsync and async in angular2 testing

妖精的绣舞 提交于 2019-11-27 03:48:34
问题 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 Fake Service or Stub in both the scenarios. 回答1: For the most part they can be used interchangeably. I can't think of anything off the top of my head in which one is required over the other, except for the case of components whose external templates and

Angular 2 Final Release Router Unit Test

十年热恋 提交于 2019-11-26 19:58:05
问题 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, TestComponentBuilder } from 'angular2/testing'; import { RootRouter } from 'angular2/src/router/router'; import { Location, RouteParams, Router, RouteRegistry, ROUTER_PRIMARY_COMPONENT } from 'angular2/router'; import { SpyLocation } from 'angular2/src/mock/location_mock'; import {

Testing error case with observables in services

佐手、 提交于 2019-11-26 17:39:24
问题 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); }, (error: Error) => console.error(error) ); }; }; The subscribe call takes two anonymous functions as parameters, I've managed to set up a working unit test for the data function but Karma won't accept coverage for the error one. I've tried spying on the console.error function, throwing an error and then expecting

Angular 2 unit testing components with routerLink

守給你的承諾、 提交于 2019-11-26 17:31:14
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 of the ListComponent template <a *ngFor="let item of data.list" class="box" routerLink="/settings/{{collectionName}}/edit/{{item._id}}"> And here is my test. import { TestBed } from '@angular/core/testing'; import { ListComponent } from './list.component'; import { defaultData, collectionName } from '../../config'; import { initialState } from '../..

Testing - Can&#39;t resolve all parameters for (ClassName)

﹥>﹥吖頭↗ 提交于 2019-11-26 11:26:52
问题 Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService \'s constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher); MetaManager is an injectable service that handles api\'s metadatas. ApiConnectorService is a service which is wrapping Http to add our custom headers and signature system. EventDispatcher is basically Symfony\'s event

Angular 2 Testing - Async function call - when to use

独自空忆成欢 提交于 2019-11-26 10:59:49
问题 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], }); }); And when do you use this? beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [MyModule], schemas: [NO_ERRORS_SCHEMA], }); })); Can anyone enlighten me on this ? 回答1: async will not allow the next test to start until the async finishes all its tasks. What async

Angular 2 unit testing components with routerLink

时光怂恿深爱的人放手 提交于 2019-11-26 05:27:37
问题 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 of the ListComponent template <a *ngFor=\"let item of data.list\" class=\"box\" routerLink=\"/settings/{{collectionName}}/edit/{{item._id}}\"> And here is my test. import { TestBed } from \'@angular/core/testing\'; import { ListComponent } from