I have an abstract class DatabaseManager, and an implementation DatabaseManagerImpl, the latter is annotated like this:
@LazySingleton(as: DatabaseManager)
class DatabaseManagerImpl implements DatabaseManager {
A lot of other classes then depend on DatabaseManager like this:
@LazySingleton()
class SomeClass {
const SomeClass(this._databaseManager);
final DatabaseManager _databaseManager;
I'm trying to add integration testing - as in, testing multiple modules at the same time, not the one where you run the app on a device. Basically, I'm testing how the backend executes certain features. Most of the backend classes depend on DatabaseManager (it initializes and provides the database connection).
First of all, I want to initialize an in-memory database for testing and fill it with fake values, so I need to mock DatabaseManager. The second reason is that I need to use sqflite_ffi because the tests are run on Linux.
The problem is, I don't know where to put DatabaseManagerMock: under the lib or test folder?
- if I put it under the
testfolder (which is where it belongs ideally), I can't generate injectable config for it because it (obviously) doesn't get included into the generated file underlib. I had an idea to generate a second injectable config file for tests and then run them in this order:
await configureDependencies() // from the main project
await configureTestDependencies() // from tests
But injectable breaks when I do that: it can't generate proper import paths for the test directory.
- Putting
DatabaseManagerMockunder the lib folder. Since I needsqflite_ffionly for testing, it should be a dev dependency, but that gives a depend_on_referenced_packages warning when I import it to use inDatabaseManagerMock.
What is the proper way to do this? I think I might be overthinking and there is a completely different approach to this that I'm missing.
I fixed the problem with the generated imports for injectable and the PR was merged yesterday, so now it's possible to do this:
Create a second injectable initializer under the
testdirectory:Then create the mock classes under
test/and set their environment totestEnv. Their corresponding non-mock classes (inlib/) must be within some other environment, likemainEnv. This is not important for the classes that are not mocked. Example:Then
configureTestDependencies()generates get_it initializer that includes all the annotated classes fromlib/plus the new mock classes.