testbed

Ionic 4: Creating mock storage

纵然是瞬间 提交于 2019-12-10 23:03:17
问题 I am trying to use Testbed in a new Angular 7 / Ionic 4 app but cannot run any tests because my components depend on an Ionic native plugin, storage. app.component.spec.ts import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import {TestBed, async, fakeAsync, tick} from '@angular/core/testing'; import { Platform } from '@ionic/angular'; import { SplashScreen } from '@ionic-native/splash-screen/ngx'; import { StatusBar } from '@ionic-native/status-bar/ngx'; import { AppComponent } from '.

Angular 2 testing - process.env

北战南征 提交于 2019-12-07 22:24:19
问题 I try to mock requests in my application, but there is a problem with process variable. I store in process.env.backendUrl url to backend API. And then in RestService I have: constructor(private http: Http) { this.rest = process.env.backendUrl + "/api/"; } And now it is impossible to run tests because in for example LoginComponent I have RestService dependency and I've got this error: zone.js:140 Uncaught Error: Error in ./LoginComponent class LoginComponent_Host - inline template:0:0 caused

Angular 2 testing - process.env

独自空忆成欢 提交于 2019-12-06 13:13:03
I try to mock requests in my application, but there is a problem with process variable. I store in process.env.backendUrl url to backend API. And then in RestService I have: constructor(private http: Http) { this.rest = process.env.backendUrl + "/api/"; } And now it is impossible to run tests because in for example LoginComponent I have RestService dependency and I've got this error: zone.js:140 Uncaught Error: Error in ./LoginComponent class LoginComponent_Host - inline template:0:0 caused by: process is not defined ReferenceError: process is not defined at new RestService (http://localhost

OverrideComponent with TestBed

こ雲淡風輕ζ 提交于 2019-12-06 05:47:33
问题 I have MainComponent that uses ChildComponentA as a @ViewChild . MainComponent is calling a method on ChildComponentA . I want to write an unit test case mocking ChildComponentA . How can I do this using TestBed (in Angular 2 RC5)? Before I used to use overrideDirective(MainComponentName, ChildComponentA, MockChildComponentA); Is there an equivalent to this using TestBed ? I tried using TestBed.overrideComponent(ChildComponentA,{ set: { template: '<div></div>' } }); which just sets the

OverrideComponent with TestBed

倾然丶 夕夏残阳落幕 提交于 2019-12-04 11:33:15
I have MainComponent that uses ChildComponentA as a @ViewChild . MainComponent is calling a method on ChildComponentA . I want to write an unit test case mocking ChildComponentA . How can I do this using TestBed (in Angular 2 RC5)? Before I used to use overrideDirective(MainComponentName, ChildComponentA, MockChildComponentA); Is there an equivalent to this using TestBed ? I tried using TestBed.overrideComponent(ChildComponentA,{ set: { template: '<div></div>' } }); which just sets the template, but I want to mock the methods in the component as well. I think in this case you can try and

Using Angular2 TestBed to Mock a service with a non-concrete class interface parameter

风流意气都作罢 提交于 2019-12-04 03:46:53
问题 I have a component that I am trying to setup and test using TestBed. This component contains one class that has a parameter in the constructor that is an interface, not a concrete class. This interface is satisfied by whatever class I choose to use (either the real one, or a mok one for unit testing). But when I am constructing the component that uses this service in TestBed, I cannot figure out how to define that parameter to the TestBed configuration. Here is the TestBed config for the

Angular 2: How to mock ChangeDetectorRef while unit testing

爱⌒轻易说出口 提交于 2019-12-03 16:29:30
问题 I have just started with Unit-Testing, and I have been able to mock my own services and some of Angular and Ionic as well, but no matter what I do ChangeDetectorRef stays the same. I mean which kind of sorcery is this? beforeEach(async(() => TestBed.configureTestingModule({ declarations: [MyComponent], providers: [ Form, DomController, ToastController, AlertController, PopoverController, {provide: Platform, useClass: PlatformMock}, { provide: NavParams, useValue: new NavParams({data: new

Angular 2: How to mock ChangeDetectorRef while unit testing

冷暖自知 提交于 2019-12-03 06:47:54
I have just started with Unit-Testing, and I have been able to mock my own services and some of Angular and Ionic as well, but no matter what I do ChangeDetectorRef stays the same. I mean which kind of sorcery is this? beforeEach(async(() => TestBed.configureTestingModule({ declarations: [MyComponent], providers: [ Form, DomController, ToastController, AlertController, PopoverController, {provide: Platform, useClass: PlatformMock}, { provide: NavParams, useValue: new NavParams({data: new PageData().Data}) }, {provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock} ], imports: [

Setting up TDD in ionic2

情到浓时终转凉″ 提交于 2019-12-01 05:29:42
问题 I've followed Joshua Moroney's tutorial on Ionic2 and TDD, but have got stuck when trying to debug errors as they occur. The core example is this one: import { TestBed, ComponentFixture, async } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { DebugElement } from '@angular/core'; import { IonicModule, NavController } from 'ionic-angular'; import { MyApp } from '../../app/app.component'; import { TabsPage } from './tabs'; let comp: TabsPage; let fixture:

What is the difference between testbed.get and inject in Angular 2/Jasmine testing?

只谈情不闲聊 提交于 2019-12-01 02:14:53
I am new to Angular 2 testing. I am trying to figure out what is the difference in using testsbed.get() and just using inject at the test level. eg: beforeEach(() => { TestBed.configureTestingModule({ providers: [SomeService] }); const testbed = getTestBed(); someService= testbed.get(SomeService); }); }); vs it('test service', inject([SomeService], (someService: SomeService) => { inject helper function was historically used since AngularJS as an alternative to direct injector calls. In Angular 1, it was necessary to bootstrap a test with ngMock . It is entirely optional in Angular 2 and higher