chrisgriffith / ionic-native-mocks

Ionic Native Mocks are designed to be used as placeholders during development for the actual Ionic Native modules.
205 stars 42 forks source link

Mocking Camera for QR-scanner #3

Closed amnonjw closed 7 years ago

amnonjw commented 7 years ago

Hello, I want to create a unit test for QR scanner, for an ionic Android app. Since it requires a camera I was thinking of using this ionic-native-mocks plugin. However, from ionic-native/barcode-scanner I can only use the scan() method which automatically opens a camera on an Android emulator.

So my questions are:

Does using the ionic-native-mocks eliminates the requirement for an emulator to start the native interface for the barcode-scanner?

I currently use Jasmine and Karama for running unit tests. Do I need end-to-end testing like Protractor if I want to use ionic-native-mocks?

The barcode-scanner can chose between a front and a back camera, but I don't know how to tell him to use a mock camera, unless I somehow register the ionic-native-mocks/camera as a front camera. Would I still need to run this on an emulator?

chrisgriffith commented 7 years ago

The mock will replace the existing native plugin and any calls to any native code. By swapping in the mock, your code will get the 'results' from the mock and NOT the device. For example, with the barcode scanner, you can insert your expected data to be returned from a successful scan. This is what I was doing when I started this entire effort. By calling the mock, the camera was never requested, and I did not need to test on device, I could keep developing locally. The idea behind the mocks is to NOT have to use the emulator or device until you really need to.

As for changing your testing methods, you should not need to change anything. These mocks only replace the calls to the actual native plugins.

See my original blog post on using the mocks https://chrisgriffith.wordpress.com/2017/07/05/announcing-ionic-native-mocks/

You can set which camera by using the BarCodeOptions object (not that it changes anything).

amnonjw commented 7 years ago

Thank you for the reply. I was originally thinking of using the mock to create several tests, where the image of the QR will change and the handling code will have to produce the correct result depending on the image.

Since I'm only now getting familiar with mocks and tests I realized I will wait with this kind of end-to-end tests for later in the development.