My team and I have been writing unit tests for our Flutter App. We've used mockito to write unit tests for our providers. Now we're trying to write unit tests for database calls. How can we test our database calls?
1 Answer
This might be what you need. Using the sqflite_ffi package to create your database.
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
Future main() async {
late Database database;
// Setup sqflite_common_ffi for flutter test
setUpAll(() {
// Initialize FFI
sqfliteFfiInit();
// Change the default factory for unit testing calls for SQFlite
databaseFactory = databaseFactoryFfi;
database = Database();
});
tearDownAll(() {
// Maybe delete the database here
});
// Tests here
test('Example test'(){
//Do your database calls here, query, delete, etc
});
}
3 Comments
Mikebarson
Thanks! To clarify on the
tests here section we can write our regular db query? Also, where do we initialize the database?rapaterno
ah yes the
test('Tests here',(){}); Let me edit itMikebarson
Adding this reference here - github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/…