Skip to content

"Bad state: This database has already been closed" error in unit-tests #1395

Answered by simolus3
rubiktubik asked this question in Q&A
Discussion options

You must be logged in to vote

Opening the database in setUp and closing it in tearDown should work. I'm using the same approach for tests in this package, and it's working fine:

https://github.com/simolus3/moor/blob/a16b34c8d06c919dcd175a399c11b8c48da34bce/moor/test/integration_tests/moor_files_integration_test.dart#L10-L18

My best guess is that you're somehow still using the old database in the second test? Are you storing the database in a top-level or static field somehow? Your test code looks reasonable though.

But also, shouldn't saveWorkout return a future and be awaited in your test? If you're not awaiting everything, it may be that the test completes before saveWorkout is done. In that case the database may be…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@rubiktubik
Comment options

@simolus3
Comment options

Answer selected by rubiktubik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants