Ionic 4: Creating mock storage

后端 未结 3 1205
忘掉有多难
忘掉有多难 2021-01-12 07:28

I am trying to use Testbed in a new Angular 7 / Ionic 4 app but cannot run any tests because my components depend on an Ionic native plugin, storage.

app.com

3条回答
  •  逝去的感伤
    2021-01-12 07:52

    First, import Storage:

    import { Storage } from '@ionic/storage';
    

    Create a const for mock:

     const storageIonicMock: any = {
         get: () => new Promise((resolve, reject) => resolve('As2342fAfgsdr')),
         set: () => ...
        };
    

    Configure your TesBed

    TestBed.configureTestingModule({
          imports: [],
          providers: [
            {
              provide: Storage,
              useValue: storageIonicMock
            }
          ]
        });
    

提交回复
热议问题