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

Issue

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?

Solution

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.

Answered By – vaind

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *