How to do Unit testing in Object Box for flutter project?
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.
raphire
Updated on November 25, 2022Comments
-
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 about 2 yearsCan you use the real ObjectBox database with a test database (another file name)?
-
raphire about 2 yearsDo 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 about 2 yearsEven 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 about 2 yearsShould look something like this:
final store = openStore(directory: 'myTestDirectory')
-