【问题标题】:How to trigger Ionic's Platform.ready in testing?如何在测试中触发 Ionic 的 Platform.ready?
【发布时间】:2017-07-21 15:16:21
【问题描述】:

我正在构建我的第一个 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();
    });
}

在一个简单的测试中,我正在检查statusBarstyleDefault 方法是否已被调用,但我还没有弄清楚如何触发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 的组件因素的错误。我仍在尝试解决该错误,但我不确定它是否与解决承诺有关。如果你有解决这个小问题的办法,我也很乐意看到。

【问题讨论】:

    标签: javascript angular ionic3 jasmine2.0


    【解决方案1】:

    您的ready Promise 永远不会解决,您需要保留platform 依赖项并强制它解决:

    const plaftorm = TestBed.get(Platform);
    spyOn(plaftorm , 'ready').and.callFake(() => Promise.resolve(''));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多