0
votes

I'am developping an app and while testing it with Jasmine and Karma I get this error but only for one page :

TypeError: Cannot read property 'subscribe' of undefined at new Content (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) at 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) at 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) at 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) at Object.debugCreateRootView [as createRootView] (webpack:///node_modules/@angular/core/esm5/core.js:14041:0 <- test-config/karma-test-shim.js:14310:12) at ComponentFactory_.create (webpack:///node_modules/@angular/core/esm5/core.js:10960:25 <- test-config/karma-test-shim.js:11229:46) at initComponent (webpack:///node_modules/@angular/core/esm5/testing.js:1086:0 <- test-config/karma-test-shim.js:46617:49) at ZoneDelegate.invoke (webpack:///node_modules/zone.js/dist/zone.js:392:0 <- test-config/karma-test-shim.js:163677:26) at ProxyZoneSpec.onInvoke (webpack:///node_modules/zone.js/dist/proxy.js:79:0 <- test-config/karma-test-shim.js:166578:39) at 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) at ZoneDelegate.invoke (webpack:///node_modules/zone.js/dist/zone.js:391:0 <- test-config/karma-test-shim.js:163676:32) at Zone.run (webpack:///node_modules/zone.js/dist/zone.js:142:0 <- test-config/karma-test-shim.js:163427:43) at NgZone.run (webpack:///node_modules/@angular/core/esm5/core.js:4446:47 <- test-config/karma-test-shim.js:4715:69) at TestBed.createComponent (webpack:///node_modules/@angular/core/esm5/testing.js:1089:0 <- test-config/karma-test-shim.js:46620:58) at Function.TestBed.createComponent (webpack:///node_modules/@angular/core/esm5/testing.js:808:0 <- test-config/karma-test-shim.js:46339:29) at UserContext.beforeEach (webpack:///src/pages/list-master/list-master.spec.ts:48:20<- test-config/karma-test-shim.js:188450:82) at ZoneDelegate.invoke (webpack:///node_modules/zone.js/dist/zone.js:392:0 <- test-config/karma-test-shim.js:163677:26) at ProxyZoneSpec.onInvoke (webpack:///node_modules/zone.js/dist/proxy.js:79:0 <- test-config/karma-test-shim.js:166578:39) at ZoneDelegate.invoke (webpack:///node_modules/zone.js/dist/zone.js:391:0 <- test-config/karma-test-shim.js:163676:32) at Zone.run (webpack:///node_modules/zone.js/dist/zone.js:142:0 <- test-config/karma-test-shim.js:163427:43) at UserContext. (webpack:///node_modules/zone.js/dist/jasmine-patch.js:104:0 <- test-config/karma-test-shim.js:166786:34) at webpack:///node_modules/@angular/core/esm5/testing.js:93:0 <- test-config/karma-test-shim.js:45624:17 at 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 <- test-config/karma-test-shim.js:166876:39) at ProxyZoneSpec.onInvoke (webpack:///node_modules/zone.js/dist/proxy.js:76:0 <- test-config/karma-test-shim.js:166575:39) at ZoneDelegate.invoke (webpack:///node_modules/zone.js/dist/zone.js:391:0 <- test-config/karma-test-shim.js:163676:32) at 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) at webpack:///node_modules/zone.js/dist/async-test.js:38:0 <- test-config/karma-test-shim.js:166865:31 at ZoneDelegate.invokeTask (webpack:///node_modules/zone.js/dist/zone.js:425:0 <- test-config/karma-test-shim.js:163710:31) at Zone.runTask (webpack:///node_modules/zone.js/dist/zone.js:192:0 <- test-config/karma-test-shim.js:163477:47) at ZoneTask.invokeTask (webpack:///node_modules/zone.js/dist/zone.js:499:0 <- test-config/karma-test-shim.js:163784:34) at 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)

I tried the solutions offered by the following topics :

I'm new to AngularJS and Ionic in general so I don't have much experience. I did some mocks to provide for my spec file as you can see here :

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();
});

And here is the ionViewDidLoad of my list-master.ts file that creates the error ( well at least I guess it comes from here) :

    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);
            });
        });
    });

If anyone had an idea of what this error is about and could help me it would be great ! Thank you all !

1

1 Answers

0
votes

Fixed it by adding :

public writeReady = {
        subscribe() {

        }

in my ViewControllerMock