2017-07-21 3 views
0

私は最初のIonicアプリを構築しており、TDDに従うことを頑張っています。私は、Ionicが提供しているPlatform.readyの約束を抱いてぶつかり合った。私は、私の人生のために、テスト中にそれを引き起こす方法を理解することはできません。イオンのデモでは、initializeAppこのような関数に表示されます:私はstatusBarstyleDefaultメソッドが呼び出されているかどうかをチェックしています簡単なテストでIonicのPlatform.readyをテストする方法を教えてください。

initializeApp() { 
    this.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. 
     this.statusBar.styleDefault(); 
     this.splashScreen.hide(); 
    }); 
} 

が、私はトリガーする方法を把握するためには至っていません解決するにはplatform.ready編集:テストファイル全体が含まれているかどうかについての質問を止めるために。

import { async, TestBed } from '@angular/core/testing'; 
import { IonicModule } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { SplashScreenMock as SplashScreen, StatusBarMock as StatusBar, PlatformMock as Platform } from '../../test-config/mocks-ionic'; 
import { LoginPageMock as LoginPage } from "../../test-config/custom-mocks/login.mock" 

describe('App Component',() => { 
    let fixture, component, SB; 

    beforeEach(async(() => { 
     TestBed.configureTestingModule({ 
      declarations:[MyApp, LoginPage], 
      imports: [IonicModule.forRoot(MyApp)], 
      providers :[StatusBar, SplashScreen, Platform] 
     }).compileComponents(); 
    })); 

    beforeEach(() => { 
     fixture = TestBed.createComponent(MyApp); 
     SB = TestBed.get(StatusBar); 
     spyOn(SB, 'styleDefault').and.callThrough(); 
     component = fixture.componentInstance; 
    }); 

    describe('general status before initialization',() => { 
     it('should be defined',() => { 
     expect(component).toBeDefined(); 
     }); 

     it('should be created',() => { 
      expect(component instanceof MyApp).toBe(true); 
     }); 

     it('should have a populated pages array',() => { 
      expect(component.pages.length).toBeGreaterThan(0); 
     }); 
    }); 

    describe('general status after initialization',() => { 
     it('should style the statusbar when the app is initialized', async((done) => { 
      component.initializeApp(); 
      expect(SB.styleDefault).toHaveBeenCalled(); 
      done(); 
     })); 
    }); 
}); 

私はおそらく間違ってそれを保持しています、または多分私が持っていますかわからないが、この時点で私が間違って何であるか見当がつかない。任意のオプションとすべてのオプションが考慮され、すべてのサポートが評価されます。

注:はい、私はfixture.detectChanges()および/またはfixture.autoDetectChanges(true)を入力しようとしましたが、未処理の約束拒否とLoginPageのコンポーネント要素が見つかりませんでした。私はまだそのエラーに対処しようとしていますが、それが約束を解決することと関係があるかどうかはわかりません。あなたがこの小さな問題の解決策を持っていれば、私もそれを見てうれしいです。

答えて

0

私はこれもかなり新しいです。 私はノートを作って、これをまだ試していません。

import {TestBed, async, ComponentFixture} from '@angular/core/testing'; 
import {AppComponent} from './app.component'; 
import {TestComponent} from './test.component'; 
import {DebugElement} from '@angular/core'; 
import {By} from '@angular/platform-browser'; 

describe('AppComponent',() => { 

次の2つのbeforeEachプリアンブル持っている必要があります:テストは、あなたがそれをログに記録慰めることができます失敗する場合

beforeEach(() => { 
    fixture = TestBed.createComponent(AppComponent); 
    component = fixture.debugElement.componentInstance; 
    el = fixture.debugElement; 
    const testField = el.query(By.css('<myCSSFilter>')); 

    fixture.detectChanges(); //Force content projection 
}); 

:次に

beforeEach(
    async(() => { 
    TestBed.ConfigureTestingModule({ 
     declarations: [ 
     ], 
     }).compileComponents(); 
    }) 
); 

をのようなものを。
強制しないと、コンテンツの投影によって時々あなたを倒すことがあります。

if('<your test here>', async(() => { 
    console.log(testField.nativeElement.outerHTML); 
    expect(testField).query(By.css('<your filter here>'))).toBeTruthy(); 
})); 

-

}); 

- このことができますなら、私を知ってみましょう。..

PS ... IIRCヨールは非同期で動作させる場合のために、beforeEachの最初の非同期を必要としています。それは別の共通の落とし穴です。

+0

申し訳ありませんが、これは役に立たず、約束を解決する方法の主な質問トピックは扱いません。通常は 'done'を渡してからそれを呼び出すのですが、何らかの理由でこれをやっていないのです。 – MBielski

+0

あなたはbeforeEachにasyncを持っていますか? fixture.detectChanges()? – JGFMK

+0

テスト設定の大部分を除外していますが、それは無関係ですが、はい、私のbeforeEachはあなたのものに非常によく似ています。 fixture.detectChangesを使用するのは、UIの変更を確認するためのものです。これは私がやろうとしていることではありません。私はこのメソッド呼び出しを検出することができます私は他人を検出することができますし、私のアプリが正常に動作していることを知っているが、私はこの1つを検出することはできません。 – MBielski

関連する問題