我正在构建我的第一个Ionic应用程序并努力遵循TDD。我已经在平台上遇到了绊脚石。我已承诺Ionic提供。在我的生活中,我无法弄清楚如何在测试时触发它。在Ionic演示中,它出现在MongoClient
函数中,如下所示:
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();
});
}
的{{1}}方法,但我还没弄明白如何触发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();
}));
});
});
,我收到有关未处理的承诺拒绝且未找到LoginPage的组件因素的错误。我仍然试图解决这个错误,但我不确定它是否与解决这个承诺有关。如果你有一个解决这个小打嗝的方法,我也很乐意看到它。
答案 0 :(得分: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', () => {
你需要有两个beforeEach前导码:
beforeEach(
async( () => {
TestBed.ConfigureTestingModule({
declarations: [
],
}).compileComponents();
})
);
然后像:
beforeEach( () => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.debugElement.componentInstance;
el = fixture.debugElement;
const testField = el.query(By.css('<myCSSFilter>'));
fixture.detectChanges(); //Force content projection
});
如果测试失败,您可以登录控制台 不强制内容投射有时会让你失望。
if('<your test here>', async( () => {
console.log(testField.nativeElement.outerHTML);
expect(testField).query(By.css('<your filter here>'))).toBeTruthy();
}));
-
});
- 如果这有帮助,请告诉我..
PS .. IIRC你需要在beforeEach中首先进行异步,因为如果异步工作。这是另一个常见问题。
答案 1 :(得分:0)
您的ready
承诺从未解决,您需要保留platform
依赖项并强制其解决:
const plaftorm = TestBed.get(Platform);
spyOn(plaftorm , 'ready').and.callFake(() => Promise.resolve(''));