Unit test issue when using LoadingController


#1

Hi, I’m using the LoadingController to show a message when retrieving some data, which works fine, but when I run my unit tests I get TypeError: controller.componentOnReady is not a function. I know it’s related to the LoadingController, because if I remove it from the page, the tests run fine. I did think it was because the LoadingController uses a Promise which I wasn’t fulfilling, but when I added the then statement to the LoadingController call the test still fails.

this is how I implemented the Loader:

 this.loader
            .create({ spinner: 'hide', message: 'Loading Messages' })
            .then(() => {
                console.log('Loading');
            });

I’m not sure what the error message is trying to tell me? Can anyone shed some light on this?

Thanks,

Stephen


#2

Actually I’ve found out what the problem was. I needed to mock up the create and dismiss functions in my Provider for the test, like this:

 providers: [
                MessageService,
                {
                    provide: LoadingController,
                    useValue: {
                        create: () => Promise.resolve(),
                        dismiss: () => Promise.resolve()
                    }
                }
            ]

The create and dismiss functions never completed because they return Promises, but failed due to the test not creating the Overlay you’d expect to see when the loader is running.