Ionic3测试:TypeError:无法读取属性'订阅'未定义的

时间:2018-03-27 13:06:10

标签: angularjs ionic-framework ionic3 karma-jasmine

我正在开发应用程序,在使用Jasmine和Karma进行测试时,我收到此错误,但仅限于一页:

  
    

TypeError:无法读取属性'订阅'未定义的                 在新内容(webpack:///node_modules/ionic-angular/components/content/content.js:218:0< - test-config / karma-test-shim.js:37929:57)                 at 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)                 在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)                 at createRootView(webpack:///node_modules/@angular/core/esm5/core.js:13339:0< - test-config / karma-test-shim.js:13608:5)                 at 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)                 在ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone.js:392:0< - test-config / karma-test-shim.js:163677:26)                 在ProxyZoneSpec.onInvoke(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-config / 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)                 在ZoneDelegate.invoke(webpack:///node_modules/zone.js/dist/zone.js:392:0< - test-config / karma-test-shim.js:163677:26)                 在ProxyZoneSpec.onInvoke(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)                 在Zone.run(webpack:///node_modules/zone.js/dist/zone.js:142:0< - test-config / karma-test-shim.js:163427:43)                 在UserContext。 (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)                 在AsyncTestZoneSpec.onInvoke(webpack:///node_modules/zone.js/dist/async-test.js:49:0< - test-config / karma-test-shim.js:166876:39)                 在ProxyZoneSpec.onInvoke(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)                 在Zone.run(webpack:///node_modules/zone.js/dist/zone.js:142:0< - test-config / karma-test-shim.js:163427:43)                 at AsyncTestZoneSpec._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/async-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 / karma-test-shim.js:163477:47)                 在ZoneTask.invokeTask(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)                 at timer(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);
            });
        });
    });

如果有人知道这个错误是什么,可以帮助我,这将是伟大的! 谢谢大家!

1 个答案:

答案 0 :(得分:0)

通过添加:

修正了它
public writeReady = {
        subscribe() {

        }

在我的ViewControllerMock