Ionic3测试:TypeError:无法读取undefined的属性'subscribe'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ionic3测试:TypeError:无法读取undefined的属性'subscribe'相关的知识,希望对你有一定的参考价值。
我正在开发一个应用程序,并在使用Jasmine和Karma进行测试时出现此错误,但仅限于一页:
TypeError:无法在新内容中读取undefined属性'subscribe'(webpack:///node_modules/ionic-angular/components/content/content.js:218:0 < - test-config / karma-test-shim.js: 37929:57)在createClass(webpack:///node_modules/@angular/core/esm5/core.js:12174:0 < - test-config / karma-test-shim.js:12443:20)的createDirectiveInstance(webpack) :///node_modules/@angular/core/esm5/core.js:12011:21 < - test-config / karma-test-shim.js:12280:37)at createViewNodes(webpack:/// node_modules / @ angular /core/esm5/core.js:13449:37 < - test-config / karma-test-shim.js:13718:53)来自callViewAction(webpack:///node_modules/@angular/core/esm5/core.js :13881:0 < - test-config / karma-test-shim.js:14150:13)at execComponentViewsAction(webpack:///node_modules/@angular/core/esm5/core.js:13790:0 < - test- config / karma-test-shim.js:14059:13)在createViewNodes(webpack:///node_modules/@angular/core/esm5/core.js:13477:0 < - test-config / karma-test-shim。 js:13746:5)在createRootView(webpack:/// node_modules / @ angular / core / e) sm5 / core.js:13339:0 < - test-config / karma-test-shim.js:13608:5)在callWithDebugContext(webpack:///node_modules/@angular/core/esm5/core.js:14740: 25 < - test-config / karma-test-shim.js:15009:42)在Object.debugCreateRootView [as createRootView](webpack:///node_modules/@angular/core/esm5/core.js:14041:0 < - test-config / karma-test-shim.js:14310:12)在ComponentFactory_.create(webpack:///node_modules/@angular/core/esm5/core.js:10960:25 < - test-config / karma -test-shim.js:11229:46)在initComponent(webpack:///node_modules/@angular/core/esm5/testing.js:1086:0 < - test-config / karma-test-shim.js:46617 :49)在ProxyZoneSpec.onInvoke的ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone.js:392:0 < - test-config / karma-test-shim.js:163677:26) (webpack:///node_modules/zone.js/dist/proxy.js:79:0 < - test-config / karma-test-shim.js:166578:39)在ZoneDelegate.invoke(webpack:/// node_modules) /zone.js/dist/zone.js:391:0 < - test-config / karma-test-shim.js:163676:32)at Object.onInvoke(webpack:/// node) _modules/@angular/core/esm5/core.js:4629:0 < - test-config / karma-test-shim.js:4898:33)在ZoneDelegate.invoke(webpack:///node_modules/zone.js/ dist / zone.js:391:0 < - test-config / karma-test-shim.js:163676:32)在Zone.run(webpack:///node_modules/zone.js/dist/zone.js:142 :0 < - test-config / karma-test-shim.js:163427:43)在NgZone.run(webpack:///node_modules/@angular/core/esm5/core.js:4446:47 < - test- test / karma-test-shim.js:4715:69)在TestBed.createComponent(webpack:///node_modules/@angular/core/esm5/testing.js:1089:0 < - test-config / karma-test- shim.js:46620:58)在Function.TestBed.createComponent(webpack:///node_modules/@angular/core/esm5/testing.js:808:0 < - test-config / karma-test-shim.js: 46339:29)在UserContext.beforeEach(webpack:///src/pages/list-master/list-master.spec.ts:48:20 < - test-config / karma-test-shim.js:188450:82 )在ProxyZoneSpec.onInvoke(webpack)上的ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone.js:392:0 < - test-config / karma-test-shim.js:163677:26) :/// node_m在ZoneDelegate.invoke上的odules / zone.js / dist / proxy.js:79:0 < - test-config / karma-test-shim.js:166578:39)(webpack:///node_modules/zone.js/dist /zone.js:391:0 < - test-config / karma-test-shim.js:163676:32)在Zone.run(webpack:///node_modules/zone.js/dist/zone.js:142:在UserContext上0 < - test-config / karma-test-shim.js:163427:43)。 (webpack:///node_modules/zone.js/dist/jasmine-patch.js:104:0 < - test-config / karma-test-shim.js:166786:34)在webpack:/// node_modules / @ angular / core / esm5 / testing.js:93:0 < - test-config / karma-test-shim.js:45624:17在ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone。 js:392:0 < - test-config / karma-test-shim.js:163677:26)at AsyncTestZoneSpec.onInvoke(webpack:///node_modules/zone.js/dist/async-test.js:49:0 ProxyZoneSpec.onInvoke上的< - test-config / karma-test-shim.js:166876:39)(webpack:///node_modules/zone.js/dist/proxy.js:76:0 < - test-config / karma -test-shim.js:166575:39)在ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone.js:391:0 < - test-config / karma-test-shim.js: 163676:32)在AsyncTestZoneSpec的Zone.run(webpack:///node_modules/zone.js/dist/zone.js:142:0 < - test-config / karma-test-shim.js:163427:43)。 _finishCallback(webpack:///node_modules/@angular/core/esm5/testing.js:88:0 < - test-config / karma-test-shim.js:45619:25)在webpack:/// node_modules / zone的.js / DIST / ASYN c-test.js:38:0 < - test-config / karma-test-shim.js:166865:31在ZoneDelegate.invokeTask(webpack:///node_modules/zone.js/dist/zone.js:425: 0 < - test-config / karma-test-shim.js:163710:31)在Zone.runTask(webpack:///node_modules/zone.js/dist/zone.js:192:0 < - test-config /在ZoneTask.invokeTask的karma-test-shim.js:163477:47)(webpack:///node_modules/zone.js/dist/zone.js:499:0 < - test-config / karma-test-shim.js :163784:34)在计时器上的ZoneTask.invoke(webpack:///node_modules/zone.js/dist/zone.js:488:0 < - test-config / karma-test-shim.js:163773:48) (webpack:///node_modules/zone.js/dist/zone.js:2040:0 < - test-config / karma-test-shim.js:165325:29)
我尝试了以下主题提供的解决方案:
我是AngularJS和Ionic的新手,所以我没有多少经验。我做了一些模拟来提供我的spec文件,你可以在这里看到:
describe('Page : ListMaster', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ListMasterPage],
imports: [
IonicModule.forRoot(ListMasterPage)
],
providers: [
{ provide: StatusBar, useClass: StatusBarMock },
{ provide: SplashScreen, useClass: SplashScreenMock },
{ provide: Platform, useClass: PlatformMock },
{ provide: NavController, useClass: NavMock },
{ provide: NavParams, useClass: NavMock },
{ provide: Games, useClass: GamesMock },
{ provide: User, useClass: UserMock },
{ provide: Tracking, useClass: TrackingMock },
{ provide: ViewController, useClass:ViewControllerMock },
LocalNotifications,
]
})
}));
beforeEach(() => {
fixture = TestBed.createComponent(ListMasterPage);
component = fixture.componentInstance;
});
it('Est créé', () => {
expect(component instanceof ListMasterPage).toBe(true);
});
it('appel fonction scrollToCurrentLevel', () => {
let subscribeEl = fixture.platform.resume.subscribe();
expect(fixture.scrollToCurrentLevel).toHaveBeenCalled();
});
这是我的list-master.ts文件的ionViewDidLoad创建错误(至少我猜它来自这里):
ionViewDidLoad() {
this.platform.ready().then(() => {
this.platform.resume.subscribe(() => {
this.user.setCreditsBack(3).then((response: any) => {
this.credits = this.user.getCreditsNb();
}).catch(err => {
let toastCreditErreur = this.toastCtrl.create({
message: 'erreur de recreditation',
duration: 3000,
position: 'middle'
});
toastCreditErreur.present();
console.log(err);
});
});
});
如果有人知道这个错误是什么,可以帮助我,这将是伟大的!谢谢你们 !
修正了它:
public writeReady = {
subscribe() {
}
在我的ViewControllerMock中
以上是关于Ionic3测试:TypeError:无法读取undefined的属性'subscribe'的主要内容,如果未能解决你的问题,请参考以下文章
未捕获(承诺):TypeError:无法读取未定义的属性“创建”(离子 3.9,Angularjs 5.0.3)
为啥这些 Jest 测试失败了? TypeError:无法读取未定义的属性“应用”
尝试测试获取所有路由时出现“TypeError:无法读取 null 的属性 '1'”?
角度单元测试:TypeError:无法读取未定义的属性“根”
Karma 测试 Angular - TypeError:无法读取 null 的属性“recipientAddressId”