3

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 1

2

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
    
  });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! To clarify on the tests here section we can write our regular db query? Also, where do we initialize the database?
ah yes the test('Tests here',(){}); Let me edit it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.