angular2-testing

Angular unit test input value

六月ゝ 毕业季﹏ 提交于 2019-12-03 00:57:24
I have been reading official Angular2 documentation for unit testing ( https://angular.io/docs/ts/latest/guide/testing.html ) but I am struggling with setting a component's input field value so that its reflected in the component property (bound via ngModel). The screen works fine in the browser, but in the unit test I cannot seem to be able to set the fields value. I am using below code. "fixture" is properly initialized as other tests are working fine. "comp" is instance of my component, and the input field is bound to "user.username" via ngModel. it('should update model...', async(() => {

Cannot read property 'subscribe' of undefined after running npm test (Angular 2 unit testing)

僤鯓⒐⒋嵵緔 提交于 2019-12-02 22:04:11
I've created a testing (spec) file for a component I'm testing. But when I run the test, it gives me an error saying Cannot read property 'subscribe' of undefined TypeError: Cannot read property 'subscribe' of undefined at ComponentUndertest.ngOnInit which is obvious because I have subscribed to something in my ngOnInit() method, but can I ignore the subscription during the test? Or can I fake a subscription during testing? I have googled a lot about this issue but couldn't find anything related with angular2 testing. Thanks in advance. Paul Samsotha Assuming you have a service that has a

Test pipe with dependencies on services

泄露秘密 提交于 2019-12-02 20:29:05
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 = new SanitiseHtmlPipe(new DomSanitizer()); }); it('create an instance', () => { expect(pipe)

How to test Angular2's router.navigate?

百般思念 提交于 2019-12-02 18:01:24
I've run into missing <router-outlet> messages in other unit tests, but just to have a nice isolated example, I created an AuthGuard that checks if a user is logged in for certain actions. This is the code: canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { if (!this.authService.isLoggedIn()) { this.router.navigate(['/login']); return false; } return true; } Now I want to write a unit test for this. This is how I start my test: beforeEach(() => { TestBed.configureTestingModule({ imports: [ RouterTestingModule.withRoutes([ { path: 'login', component: DummyComponent } ]) ],

How to fix beforeEachProviders (deprecated on RC4)

岁酱吖の 提交于 2019-12-01 15:02:30
Ive just upgraded Angular2 from RC3 to RC4 ... import { expect, it, iit, xit, describe, ddescribe, xdescribe, beforeEach, beforeEachProviders, withProviders, async, inject } from '@angular/core/testing'; In my unit test I have the following code ... beforeEachProviders(() => [ {provide: Router, useClass: MockRouter} ]); This works fine but since moving to RC4 I have a deprecation warning on beforeEachProviders . Anyone know what the new way of doing things is? Or should I import beforeEachProviders from somewhere else instead of '@angular/core/testing'? mifish You will need to import

The pipe 'translate' could not be found , angular2 component testing

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:51:10
I am working on component testing with angular2. in my html template i use the translate pipe. This is the code of the test : import { ComponentFixture, TestBed ,getTestBed} from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { DebugElement } from '@angular/core'; import { RightComponent } from './right.component'; import {TranslateService} from 'ng2-translate/ng2-translate'; import {Injector} from "@angular/core"; let comp: RightComponent; let fixture: ComponentFixture<RightComponent>; let el: DebugElement; let translate: TranslateService; let injector:

Unit testing in angular2, dependency injection

亡梦爱人 提交于 2019-11-30 23:43:02
问题 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 difference. Struggling in app.component.ts already, which contains my navigation bits. Relevant bits of the template here: <nav class="navbar navbar-light bg-faded"> <div class="container"> <div class="nav navbar-nav"> <a class="navbar-brand" [routerLink]=" ['./']

Unhandled Promise rejection: Cannot match any routes

╄→尐↘猪︶ㄣ 提交于 2019-11-30 11:42:26
When I run this unit test: it('can click profile link in template', () => { const landingPageLinkDe = linkDes[0]; const profileLinkDe = linkDes[1]; const aboutLinkDe = linkDes[2]; const findLinkDe = linkDes[3]; const addLinkDe = linkDes[4]; const registerLinkDe = linkDes[5]; const landingPageLinkFull = links[0]; const profileLinkFull = links[1]; const aboutLinkFull = links[2]; const findLinkFull = links[3]; const addLinkFull = links[4]; const registerLinkFull = links[5]; navFixture.detectChanges(); expect(profileLinkFull.navigatedTo) .toBeNull('link should not have navigated yet');

Angular2 RC5 Mock Activated Route Params

最后都变了- 提交于 2019-11-30 08:47:24
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: undefined is not a constructor (evaluating 'this.route.params.subscribe') Any help would be greatly appreciated

Unhandled Promise rejection: Cannot match any routes

跟風遠走 提交于 2019-11-29 17:16:32
问题 When I run this unit test: it('can click profile link in template', () => { const landingPageLinkDe = linkDes[0]; const profileLinkDe = linkDes[1]; const aboutLinkDe = linkDes[2]; const findLinkDe = linkDes[3]; const addLinkDe = linkDes[4]; const registerLinkDe = linkDes[5]; const landingPageLinkFull = links[0]; const profileLinkFull = links[1]; const aboutLinkFull = links[2]; const findLinkFull = links[3]; const addLinkFull = links[4]; const registerLinkFull = links[5]; navFixture