How to do Unit testing in Object Box for flutter project?

339

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.

Share:
339
raphire
Author by

raphire

Updated on November 25, 2022

Comments

  • raphire
    raphire 12 months

    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?

    • Markus Junginger
      Markus Junginger about 2 years
      Can you use the real ObjectBox database with a test database (another file name)?
    • raphire
      raphire about 2 years
      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
      raphire about 2 years
      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.
    • Markus Junginger
      Markus Junginger about 2 years
      Should look something like this: final store = openStore(directory: 'myTestDirectory')