How to test @ionic/storage?
Asked Answered
P

1

6

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?

Prunelle answered 9/1, 2017 at 14:41 Comment(2)
In terms of unit testing you don't test @ionic/storage, you test your code's interactions with it. So yes, you'd typically mock it out.Pier
Possible duplicate of Ionic 4: Creating mock storageEmalia
A
6

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.

Astyanax answered 10/1, 2017 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.