2017-04-09 115 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是看項目的鏈接真正整合了這個模擬類

希望能幫到:)