skip to Main Content

I have a test folder in my flitter project which contains the following :

group ('beneficiary methods tests', () {

test('insert beneficiary test',()async{…}

test('update beneficiary test',()async{…}

test('get beneficiary test',()async{…}

});

await deleteDatabase('testingDB');

What I’m expecting is to run the code line by line , meaning that the deletion of the database will be after each test’s execution , however this is not happening the tests depend’s on the database and they’re failing with the following error

SafLiteFfiException(error,
Bad state: This database
has already been closed})

Moving the await deleteDatabase(‘testingDB’); above the group solved the error but I still want to understand the reason of this behavior

2

Answers


  1. The async that you have in there seems to indicate that you have asynchronous code in there. Asynchronous code very explicitly does not meet your expectation that the lines of code execute in order. You might be able to use await in the group to ensure those calls have completed before you try to delete the database.

    To learn more about this, see:

    Login or Signup to reply.
  2. test registers a test callback. It does not execute the test immediately.

    If you have code that you want to be executed after all tests have run, then you must register that code as a tearDownAll callback. If you want that to happen after all tests in a group, register your tearDownAll callback within that group:

    group('...', () {
      test('...', () async {
        // ...
      });
    
      test('...', () async {
        // ...
      });
    
      tearDownAll(() async {
        await deleteDatabase('testingDB');
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search