angular2-testing

Angular2 2.0.0 Component Unit Test causes error: Bootstrap at least one component before injecting Router

此生再无相见时 提交于 2019-11-29 15:24:22
Looking at previous similar questions, it doesn't seem that there was ever a clear answer for this. Here's what's happening. I'm building an angular2 app using the angular-cli tool (beta 16 running, with angular 2.0.1 and router 3.0.1. When I run "ng test" all my tests pass, expect my app.component. (ng serve is working fine etc) Here's some code: app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '

Angular2 RC5 Mock Activated Route Params

夙愿已清 提交于 2019-11-29 12:30:03
问题 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 actual component like this: this.route.params.subscribe(params => { this.stateId = +params['id']; this.stateService.getState(this.stateId).then(state => { this.state = state; }); }); The error I get with my current attempt is simply: TypeError:

NG2 RC5: HTTP_PROVIDERS is deprecated

廉价感情. 提交于 2019-11-29 03:44:42
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, but since HTTP_PROVIDERS is deprecated, what should I be doing now? Is there something I need to provide instead of HTTP_PROVIDERS? What is the correct way to do this in the RC5 world? beforeEach(() => { reflectiveInjector = ReflectiveInjector.resolveAndCreate([ HTTP_PROVIDERS, ... ]); //other code here... }); it("should....", () => { ... }); The

Testing ngOnChanges lifecycle hook in Angular 2

一笑奈何 提交于 2019-11-29 03:00:23
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 '@angular/compiler/testing'; import {Component, OnChanges, Input} from '@angular/core'; @Component({ selector: 'test', template: `<p>{{value}}</p>`, }) export class TestComponent implements OnChanges { @Input() value: string; ngOnChanges(changes: {}): any { // should be called } } fdescribe('TestComponent', () => { let tcb: TestComponentBuilder; beforeEachProviders(() => [ TestComponentBuilder,

How to change value of a select box in angular2 unit test?

陌路散爱 提交于 2019-11-29 02:48:47
问题 I have an Angular2 component that contains a select box that looks like <select [(ngModel)]="envFilter" class="form-control" name="envSelector" (ngModelChange)="onChangeFilter($event)"> <option *ngFor="let env of envs" [ngValue]="env">{{env}}</option> </select> I am trying to write a unit test for the ngModelChange event. This is my latest failing attempt it("should filter and show correct items", async(() => { fixture.detectChanges(); fixture.whenStable().then(() => { el = fixture

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

最后都变了- 提交于 2019-11-28 06:39:48
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 <input id="name" type="text" [(ngModel)]="name" /> <div id="divName">{{name}}</div> The app.component.ts @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent implements OnInit { name: string; } app.component.spec.ts import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { AppService } from './app.service'; describe('App:

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

梦想与她 提交于 2019-11-28 03:32:19
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(params => { this.userId = params['id']; this.userObj = this._session.allUsers.filter(user => user.id

Angular 2 Final Release Router Unit Test

匆匆过客 提交于 2019-11-27 19:41:53
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 { provide } from 'angular2/core'; import { App } from './app'; describe('Router', () => { let location, router;

NG2 RC5: HTTP_PROVIDERS is deprecated

放肆的年华 提交于 2019-11-27 17:42:47
问题 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, but since HTTP_PROVIDERS is deprecated, what should I be doing now? Is there something I need to provide instead of HTTP_PROVIDERS? What is the correct way to do this in the RC5 world? beforeEach(() => { reflectiveInjector = ReflectiveInjector

Testing - Can't resolve all parameters for (ClassName)

扶醉桌前 提交于 2019-11-27 09:03:43
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 dispatcher system, in typescript. Problem When I test the ApiService , I do an initialization in beforeEach