angular2-testing

spyOn could not find an object to spy upon for start()

心已入冬 提交于 2019-12-22 04:14:19
问题 I am using angular-cli testing framework. inside my component , I have used 'ng2-slim-loading-bar' node module. submit(){ this._slimLoadingBarService.start(() => { }); //method operations } Now when I am testing this component, I have applied spyOn this service as : beforeEach(() => { let slimLoadingBarService=new SlimLoadingBarService(); demoComponent = new DemoComponent(slimLoadingBarService); TestBed.configureTestingModule({ declarations: [ DemoComponent ], providers: [ { provide:

Unit Testing angular2 component with imported module

房东的猫 提交于 2019-12-21 03:59:18
问题 I am trying to write a test on a component which uses angular-material2, but when I add it to my testModule declarations I get: Error: Template parse errors: 'md-card-title' is not a known element: 1. If 'md-card-title' is an Angular component, then verify that it is part of this module. 2. If 'md-card-title' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. Adding MaterialModule to the declarations throws `Error:

Mocking service in a component - mock ignored

自作多情 提交于 2019-12-20 18:53:46
问题 This time I'm trying to mock a service (that does http calls) to test a component. @Component({ selector: 'ub-funding-plan', templateUrl: './funding-plan.component.html', styleUrls: ['./funding-plan.component.css'], providers: [FundingPlanService] }) export class FundingPlanComponent implements OnInit { constructor(private fundingPlanService: FundingPlanService) { } ngOnInit() { this.reloadFundingPlans(); } reloadFundingPlans() { this.fundingPlanService.getFundingPlans().subscribe(

How to fix beforeEachProviders (deprecated on RC4)

谁说我不能喝 提交于 2019-12-19 14:44:40
问题 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

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

余生颓废 提交于 2019-12-18 09:04:42
问题 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 }

Angular 2 RC5 Testing Promises in ngOnInit Not Working

只愿长相守 提交于 2019-12-17 20:29:32
问题 I am trying to test a structural directive named MyDirective with Jasmine. The Angular version used is RC5. // Part of the MyDirective class @Directive({selector: '[myDirective]'}) export class MyDirective { constructor(protected templateRef: TemplateRef<any>, protected viewContainer: ViewContainerRef, protected myService: MyService) { } ngOnInit() { this.myService.getData() .then((data) => { if (!MyService.isValid(data)) { this.viewContainer.createEmbeddedView(this.templateRef); } else {

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

人盡茶涼 提交于 2019-12-17 17:59:25
问题 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';

Angular2 + jquery autocomplete Component: unable to call methods on component inside spec file

别等时光非礼了梦想. 提交于 2019-12-12 16:38:30
问题 I have jquery autocomplete method being used inside angular2 which calls service to fetch data from api. Here is myComponent.ts : export class myComponent { private myVar; private binding1; private binding2; constructor( @Inject(ElementRef) private elementRef: ElementRef,private _myService: MyService) { } public method1() { return this.myVar.val().toUpperCase(); } public method2() { return this.myVar.val(""); } private ngOnInit() { var _this = this; this.myVar = $(this.elementRef

Angular2 Component: Testing form input value change

痞子三分冷 提交于 2019-12-12 07:50:14
问题 I have a text input and i'm listening for the changes. mycomponent.ts ngOnInit() { this.searchInput = new Control(); this.searchInput.valueChanges .distinctUntilChanged() .subscribe(newValue => this.search(newValue)) } search(query) { // do something to search } mycomponent.html <search-box> <input type="text" [ngFormControl]="searchInput" > </search-box> Running the application everything works fine, but i want to unit-test it. So here's what i tried mycomponent.spec.ts beforeEach(done => {

How to test an asynchronous function using karma/Jasmine in ionic2?

*爱你&永不变心* 提交于 2019-12-12 04:03:21
问题 Below I have written down the code in which there is an asynchronous function, that uses setTimeout() internally and prints message after 3seconds. I have to write the spec using Jasmine. I have followed the docs but I didn't get how to apply this in the code. home.ts //implementing only function written in a class.All imports are done properly. click(){ function delayedAlert(message: string, time: number, cb) { return setTimeout(() => cb(message), time); } delayedAlert('Aditya3', 3000,