2017-04-09 120 views
3

我是新与Ionic2和我下面的教程this和一个简单的测试像离子2:噶/茉莉花错误检测/测试床

describe('Dummy test',() => { 

it('should do nothing',() => { 

    expect(true).toBeTruthy(); 
    expect(1 + 1).toBe(2); 

}); 

}); 

工作正常,但由于某种原因,我不断收到此错误当我尝试按照教程的其余部分。

Component: Root Component 
✖ initialises with a root page of LoginPage 
    Firefox 45.0.0 (Linux 0.0.0) 
TypeError: win is undefined in src/test.ts (line 937) 

我的src/test.ts与本教程相同,它没有任何胜利。我app.spec.ts是这个

import { TestBed, ComponentFixture, async } from '@angular/core/testing'; 
import { IonicModule } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { UserData } from '../providers/user-data'; 
import { LoginPage } from '../pages/login/login'; 
import { Platform } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { LoginPage } from '../pages/login/login'; 

let comp: MyApp; 
let fixture: ComponentFixture<MyApp>; 

describe('Component: Root Component',() => { 

beforeEach(async(() => { 

    TestBed.configureTestingModule({ 

     declarations: [MyApp], 

     providers: [ 
      StatusBar, 
      SplashScreen, 
      UserData, 
      Platform 
     ], 

     imports: [ 
      IonicModule.forRoot(MyApp) 
     ] 

    }).compileComponents(); 

})); 

beforeEach(() => { 

    fixture = TestBed.createComponent(MyApp); 
    comp = fixture.componentInstance; 

}); 

afterEach(() => { 
    fixture.destroy(); 
    comp = null; 
}); 

it('initialises with a root page of LoginPage',() => { 
    expect(comp['rootPage']).toBe(LoginPage); 
}); 

}); 

而且我app.component.ts是这个

import { Component } from '@angular/core'; 
import { Platform } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { MenuSidePage } from '../pages/menu-side/menu-side'; 
import { LoginPage } from '../pages/login/login'; 
import { UserData } from '../providers/user-data'; 


@Component({ 
    template: `<ion-nav #nav [root]="rootPage"></ion-nav>` 
}) 
export class MyApp { 

    rootPage: any; 

    constructor(
    public platform: Platform, 
    public statusBar: StatusBar, 
    public splashScreen: SplashScreen, 
    private userData: UserData, 
) { 
    platform 
     .ready() 
     .then(() => { 
     //First - check if user is logged 
     if(this.userData.currentUser) { 
      this.rootPage = MenuSidePage; 
     } else { 
      this.rootPage = LoginPage; 
     } 
     statusBar.styleDefault(); 
     splashScreen.hide(); 

    }); 
    } 
} 

回答

0

我还没有的解决方案,但你不应该使用compileComponents()因为你使用的是template而不是templateUrl就像这个tutorial说:

我们需要的时候,我们需要异步编译组件,如一个具有外部模板(一个是加载使用compileComponents通过templateUrl并且不用模板内联)。这就是运行此代码的beforeEach块使用异步参数的原因 - 它为compileComponents在内部运行设置了一个异步测试区。

希望它是一种帮助:)

0

win()功能来自Plaftorm,你要嘲笑它如下:

export class PlatformMock { 
    public ready(): Promise<string> { 
    return new Promise((resolve) => { 
     resolve('READY'); 
    }); 
    } 

    public getQueryParam() { 
    return true; 
    } 

    public registerBackButtonAction(fn: Function, priority?: number): Function { 
    return (() => true); 
    } 

    public hasFocus(ele: HTMLElement): boolean { 
    return true; 
    } 

    public doc(): HTMLDocument { 
    return document; 
    } 

    public is(): boolean { 
    return true; 
    } 

    public getElementComputedStyle(container: any): any { 
    return { 
     paddingLeft: '10', 
     paddingTop: '10', 
     paddingRight: '10', 
     paddingBottom: '10', 
    }; 
    } 

    public onResize(callback: any) { 
    return callback; 
    } 

    public registerListener(ele: any, eventName: string, callback: any): Function { 
    return (() => true); 
    } 

    public win(): Window { 
    return window; 
    } 

    public raf(callback: any): number { 
    return 1; 
    } 

    public timeout(callback: any, timer: number): any { 
    return setTimeout(callback, timer); 
    } 

    public cancelTimeout(id: any) { 
    // do nothing 
    } 

    public getActiveElement(): any { 
    return document['activeElement']; 
    } 
} 

Here是看项目的链接真正整合了这个模拟类

希望能帮到:)