angular2-testing

Angular 2 Unit Testing Observable Errors (HTTP)

怎甘沉沦 提交于 2020-01-21 07:11:34
问题 I am trying to write unit tests for my API service but have some trouble catching HTTP errors. I am following this guide along with the Angular2 docs since the guide is (slightly) out of date in some minor areas. All unit tests pass apart from those where an error is thrown by the service (due to error HTTP status code). I can tell this by logging out response.ok . From what i've read this has something to do with the unit tests not executing asynchronously, hence, not waiting for the error

Testing routerLink directive in Angular 2

断了今生、忘了曾经 提交于 2020-01-06 03:58:13
问题 I'm trying to test the work of routing. I moved navbar to a separate component - MdNavbar. Basically only html and css in there, the RouteConfig is in other component and MdNavbar is injected in there. I want to test that route changes when clicking on the link. In test I'm looking for the Profile link and clicking on it. I expect the route to change. Here is the code from my tests - import {it, inject,async, describe, beforeEachProviders, tick, fakeAsync} from '@angular/core/testing'; import

Angular 2 keeps adding “/testing” to the end of my file imports - systemjs

末鹿安然 提交于 2020-01-05 05:35:10
问题 I've just started to add unit tests to my angular 2 app and I am having a nightmare, every time I have to import something new into my app that I haven't used before, it adds "/testing" to the end of each import: eg: localhost:3002/node_modules/@angular/compiler/bundles/compiler.umd.js/testing 404 (Not Found) It is driving me nuts because I fix one of that error and then the same thing happens to another file import when I need to import something else. current full error: zone.js:1382 GET

angular 2 testing w/router

断了今生、忘了曾经 提交于 2020-01-02 07:04:58
问题 I have a component and when a user logs in it routes to a url called /dashboard I am really struggling figuring out why I am getting the following error. cannot read property 'args' of undefined I have been following the official docs on testing with router found here https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component but it doesnt seem to help. Half my problem is I dont quite understand all the code in the doc. Here is my unit test for the route beforeEach(async(()=>{

Angular2 - Unit testing Observable error “Cannot read property 'subscribe' of undefined”

只谈情不闲聊 提交于 2020-01-02 04:48:06
问题 I have a service function which is returning Observable and I am consuming that service inside one of my components. I have written a unit test for the component using the technique posted here. However, strangely I am getting "cannot read property 'subscribe' of undefined". Component under test: @Component({ moduleId: module.id, templateUrl: 'signup.component.html' }) export class SignupComponent implements OnInit { signupForm: FormGroup; submitted = false; registered = false; constructor

Test pipe with dependencies on services

落花浮王杯 提交于 2019-12-31 10:44:14
问题 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 { constructor(private _sanitizer: DomSanitizer) {} transform(value: any): any { return this._sanitizer.bypassSecurityTrustHtml(value); } } I want to test it as below: describe('Pipe: Sanatiser', () => { let pipe: SanitiseHtmlPipe; beforeEach(() => { pipe

Testing ngOnChanges lifecycle hook in Angular 2

送分小仙女□ 提交于 2019-12-29 05:41:35
问题 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(

Karma cannot load Angular 2 Component

送分小仙女□ 提交于 2019-12-23 03:25:08
问题 My Karma cannont load angular 2 component. Drop this error: [web-server] :404 : /base/src/client/app/container/container.component i do everything with angular 2 testing tutorial (i only modified path in karma) So problem must be in path. but i don't have any idea how to fix it. My Path: src | |client | | |container | | | |container.component.ts | | | |container.component.spec.ts |karma.conf.js |karma-test-shim.js container.component.ts import { Component, OnInit } from '@angular/core';

Testing a component, which depends on a route param

為{幸葍}努か 提交于 2019-12-22 06:56:01
问题 I have a question about testing a routed component in angular2. Here is a simple component, which depends on a route with a parameter 'foo' . The attribute foo in the component will be set to the value of the parameter. import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Params} from '@angular/router'; @Component({ selector: 'my-component', templateUrl: './my-component.html' }) export class MyComponent implements OnInit { foo: string; constructor( private route:

Cannot read property 'injector' of null jasmine angular 2

我只是一个虾纸丫 提交于 2019-12-22 04:46:16
问题 I'm getting this error when running a jasmine spec in angular 2: Cannot read property 'injector' of null jasmine angular 2 stack trace: TypeError: Cannot read property 'injector' of null at TestBed._createCompilerAndModule (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:834:48) at TestBed._initIfNeeded (http://localhost:3002/node_modules/@angular/core/bundles/core-testing.umd.js:800:43) at TestBed.createComponent (http://localhost:3002/node_modules/@angular/core