我正在构建我的第一个 Ionic 应用程序并努力遵循 TDD。我遇到了 Ionic 提供的 Platform.ready 承诺的绊脚石。在我的一生中,我无法弄清楚如何在测试时触发它。在 Ionic 演示中,它出现在如下initializeApp
函数中:
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();
});
}
在一个简单的测试中,我正在检查是否已调用 的styleDefault
方法statusBar
,但我还没有弄清楚如何触发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 的组件因素的错误。我仍在尝试解决该错误,但我不确定它是否与解决承诺有关。如果你有解决这个小问题的办法,我也很乐意看到。