angular2-testing

Angular2 unit testing : testing a component's constructor

瘦欲@ 提交于 2019-12-07 00:53:39
问题 All is in the title : how can one test what is done in the component's constructor ? For your information, I am using a service that requires a setting, and I would like to see if the 2 methods that I call in the constructor are called correctly. My component's constructor : constructor( public router: Router, private profilService: ProfileService, private dragula: DragulaService, private alerter: AlertService ) { dragula.drag.subscribe((value) => { this.onDrag(value); }); dragula.dragend

angular 2 testing w/router

耗尽温柔 提交于 2019-12-06 01:08:28
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(()=>{ class AuthStub{ private loggedin: boolean = false; login(){ return this.loggedin = true; } }; class

Angular 2 and jQuery - how to test?

走远了吗. 提交于 2019-12-05 12:03:34
问题 I am using Angular-CLI (webpack version) for my Angular 2 project and I also need to use jQuery (sadly. In my case, it's a dependency of Semantic-UI and I am using it for handling menu dropdowns). The way I am using it: npm install jquery --save Then listing in it angular-cli.json file in the scripts array: scripts": [ "../node_modules/jquery/dist/jquery.min.js" ] So it gets included into bundle file and this file is automatically used to root html file: <script type="text/javascript" src=

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

不羁岁月 提交于 2019-12-05 10:11:42
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(public router: Router, private fb: FormBuilder, public authService: AuthService) { this.signupForm =

Cannot read property 'injector' of null jasmine angular 2

元气小坏坏 提交于 2019-12-05 06:41:18
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/bundles/core-testing.umd.js:884:18) at Function.TestBed.createComponent (http://localhost:3002/node

Angular2 unit testing : testing a component's constructor

喜欢而已 提交于 2019-12-05 05:06:15
All is in the title : how can one test what is done in the component's constructor ? For your information, I am using a service that requires a setting, and I would like to see if the 2 methods that I call in the constructor are called correctly. My component's constructor : constructor( public router: Router, private profilService: ProfileService, private dragula: DragulaService, private alerter: AlertService ) { dragula.drag.subscribe((value) => { this.onDrag(value); }); dragula.dragend.subscribe((value) => { this.onDragend(value); }); } I would inject a fake service using the DI system,

How to unit test a FormControl in Angular2

☆樱花仙子☆ 提交于 2019-12-04 10:03:06
问题 My method under test is the following: /** * Update properties when the applicant changes the payment term value. * @return {Mixed} - Either an Array where the first index is a boolean indicating * that selectedPaymentTerm was set, and the second index indicates whether * displayProductValues was called. Or a plain boolean indicating that there was an * error. */ onPaymentTermChange() { this.paymentTerm.valueChanges.subscribe( (value) => { this.selectedPaymentTerm = value; let returnValue = [

How to mock Pipe when testing Component

蹲街弑〆低调 提交于 2019-12-04 08:50:22
问题 Currently I am overriding providers to use mocked services like this: beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { tcb.overrideProviders(AddFieldToObjectDropdownComponent, [ provide(ServiceA, { useClass: MockServiceA })), provide(ServiceB, { useClass: MockServiceB })) ])... I want to do same thing for pipes that the component uses. I tried, provide(PipeA, { useClass: MockPipeA }) and provide(PipeA, { useValue: new MockPipeA() }) but both didn't work. 回答1: You can

How do you inject an angular2 service into a unit test? (RC3)

梦想与她 提交于 2019-12-04 08:47:46
I am using RC3 . I am implementing the new Angular2 router as documented here: https://angular.io/docs/ts/latest/guide/router.html Everything works fine but I am having problem in unit testing. Specifically, I cannot inject Angular2 services into my unit tests. My relevant component code is: import {Component} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ templateUrl: ... styleUrls: ... }) export class Route1DetailComponent { constructor(private route:ActivatedRoute) { console.log(route); } } my unit test looks like: import { expect, it, iit, xit, describe,

sample code: making mocks in angular2

痞子三分冷 提交于 2019-12-04 06:22:20
问题 I am learning Angular2. In DI pages, there is sample code for mocking. https://angular.io/docs/ts/latest/guide/dependency-injection.html What does it mean let mockService = <HeroService> {getHeroes: () => expectedHeroes } It looks like define mockService function from HeroService function. What is <HeroService> ? Is <HeroService> casting? let expectedHeroes = [{name: 'A'}, {name: 'B'}] let mockService = <HeroService> {getHeroes: () => expectedHeroes } it('should have heroes when