Ionic 4: Creating mock storage
First, import Storage:
import { Storage } from '@ionic/storage';
Create a const for mock:
const storageIonicMock: any = {
get: () => new Promise<any>((resolve, reject) => resolve('As2342fAfgsdr')),
set: () => ...
};
Configure your TesBed
TestBed.configureTestingModule({
imports: [],
providers: [
{
provide: Storage,
useValue: storageIonicMock
}
]
});
The Ionic plugin storage also works in a browser environment.
You don't need to create a mock and you can directly import, define and inject it in your component (if you are running your test in a browser environment) :
First import :
import { Storage } from '@ionic/storage';
import { MyComponent } from './my-component';
Declare a global component variable :
let component: MyComponent;
Then and for each test, define and inject the storage in your component :
beforeEach(() => {
const storage = new Storage({ // Define Storage
name: '__mydb',
driverOrder: ['indexeddb', 'sqlite', 'websql']
});
component = new MyComponent(storage); // Inject Storage in your component
}