How to test @ ion / storage?

I need to check my service, where I use @ ionic / storage to get and set data in a method. Do I need to falsify the entire storage engine, or what would be better for this?

+6
source share
2 answers

In general unit testing, only the test code you write is used.

You can create a layout that is basically a class that uses the methods that you use getor set.

Then you have two options. Either you use Jasmine Spies, which allows you to make fun of the return value of those methods geteither setin your specifications, or you can directly place the return value in the actual Mock class.

The first is more ideal as it allows you to see the return value directly on specand allows for additional customization.

Spy documentation is here . I use spyOn(...).and.returnValue()or a lot, but there are many methods that you can use.

If you give more detailed information in the exact specification that you are trying to write, you can get better answers.

+6
source

First, import storage:

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

const :

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

TesBed

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

Source: https://habr.com/ru/post/1666262/


All Articles