Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test @ionic/storage?

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?

like image 674
mrp Avatar asked Jan 09 '17 14:01

mrp


1 Answers

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.

like image 184
Ka Mok Avatar answered Oct 26 '22 18:10

Ka Mok