错误:未捕获(在承诺中):无效链接:在延迟加载单元测试用例的情况下为LoginPage

时间:2018-02-17 17:47:14

标签: angular unit-testing jasmine ionic3

我的Ionic 3应用程序延迟加载一个LoginPage作为rootPage,我的菜单是我的应用程序组件。

当我为app组件编写单元测试用例并执行fixture.detectChanges();

错误:未捕获(在承诺中):无效链接:LoginPage 它引发了错误。

  

app.component.ts

    import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { Platform, NavController, Nav } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

@Component({
  templateUrl: 'app.html',
  encapsulation: ViewEncapsulation.None
})
export class MyApp {

  @ViewChild('pagecontent') navCtrl: NavController;

  rootPage: string = 'LoginPage';


  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

    });
  }

  openPage(p) {
    this.collapseAllSubMenus();
    this.navCtrl.setRoot(p);
  }

  toggleSubMenu(m) {
    console.log(newValue);
    var newValue;
    switch (m) {
      case "aaa":
        break;
      case "xxx":
        break;
    }
  }

  collapseAllSubMenus() {

  }

}
  

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { InterceptorModule } from '../interceptors/app-header-module';

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpClientModule,
    InterceptorModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}
  

app.component.spec.ts

import {TestBed, async} from '@angular/core/testing';
import {ErrorHandler} from '@angular/core';
import {IonicModule, IonicErrorHandler, Platform} from 'ionic-angular';
import {HttpClientModule} from '@angular/common/http';
import {BrowserModule} from '@angular/platform-browser';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {By} from '@angular/platform-browser';
import {Device} from '@ionic-native/device';

import {MyApp} from './app.component';
import {DeviceMock} from "@ionic-native-mocks/device";
import {PlatformMock, SplashScreenMock, StatusBarMock} from '../../test-config/mocks-ionic';

fdescribe('MyApp', () => {
  let component, fixture, de;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        MyApp
      ],
      imports: [
        BrowserModule,
        IonicModule.forRoot(MyApp),
        HttpClientModule
      ],
      providers: [
        {provide: ErrorHandler, useClass: IonicErrorHandler},
        {provide: Device, useClass: DeviceMock},
        {provide: StatusBar, useClass: StatusBarMock},
        {provide: SplashScreen, useClass: SplashScreenMock},
        {provide: Platform, useClass: PlatformMock},
      ]
    }).compileComponents();
  }));

  // synchronous beforeEach
  beforeEach(() => {
    fixture = TestBed.createComponent(MyApp);
    component = fixture.componentInstance;
    de = fixture.debugElement;
  });


  it('should create the app', async(() => {
    expect(component).toBeDefined();
  }));


  it('clicking a button should expand a menu', async(() => {
    // const btnExpandSettings = de.query(By.css("#btnExpandSettings"));
    // btnExpandSettings.triggerEventHandler("click", null);
    fixture.detectChanges();
    // let submenuSettings = de.query(By.css("#submenuSettings"));
    // let btns = submenuSettings.queryAll(By.css("button"));
    // expect(submenuSettings).not.toBeNull();
    // expect(btns.length).toBe(2);
  }));

0 个答案:

没有答案