How to test @ionic/storage?

2.8k views Asked by At

I need to test my service where I'm using @ionic/storage to get and set data in a method. Do I need to mock the whole storage mechanism or what would be the best practice for that?

1

There are 1 answers

0
Ka Mok On BEST ANSWER

In general unit testing, only test code that you write.

You can create a mock, which is basically a class that has the methods you use get or set.

Then you have two options. Either you use Jasmine's Spies, which allows you to mock the return value of those get or set methods in your specs, or you can directly place the return value in the actual Mock of the class.

The former is more ideal, as it allows you to see the return value directly on the spec and allows for more customization.

The spies documentation is here. I use the spyOn(...).and.returnValue() or a lot, but there are a variety of methods you can use.

If you give more details in the exact spec you're trying to write, you can get better answers.