1

How can we do unit testing for object box in flutter project for all crud operations?

class ShapeRepo {

final _box = store.box<ShapeModel>();

void saveShape(ShapeModel model) {
    _box.put(model);
}

This is one example. There are many box operations. I was wondering if a separate object box store is available for testing?

raphire
  • 198
  • 9
  • Can you use the real ObjectBox database with a test database (another file name)? – Markus Junginger Aug 11 '21 at 07:09
  • Do you mean that we clear all data from the store, populate it with test data and then run the box operations and query operations on the test suite separately ? – raphire Aug 11 '21 at 07:43
  • Even if I do that, do I have to run the simulator or device to provide the path for the store initialisation or is there a facility to provide the local path, such that object_box_test.mdb file is created inside test folder or something like that. – raphire Aug 11 '21 at 07:48
  • Should look something like this: `final store = openStore(directory: 'myTestDirectory')` – Markus Junginger Aug 11 '21 at 19:13

1 Answers1

2

Following Flutter's unit-testing guide, you can adapt it to your project. Say you have some classes depending on ObjectBox in the lib folder. In that case, just create your test cases for package:test as you normally would, and provide a locally opened database: just import openStore from your lib/objectbox.g.dart (or wherever you have the generated code. The database will be saved in the current directory (in the objectbox subdirectory), unless specified otherwise. It is good practice to delete this directory in the tearDown function of the test group.

Considering the class you've posted in the question - it must get the store from somewhere, right? So for example you'd give the store opened in the test folder as a constructor argument.

vaind
  • 1,642
  • 10
  • 19