Testing
Making your test a KoinComponent with KoinTest
By import koin_test.dart
, you will be able to use all available test methods:
inject()
&get()
- function to retrieve yoru instances from KointestModules()
,testModule()
&testKoinDeclaration()
- help you check your configuration. These are functions marked as isTest, that is, they are equivalent to test();declareModule()
- to declare a module to be loaded in the global context of koindeclare()
- to a instance to be loaded in the global context of koinkoinTearDown()
- Register a tearDown function that close Koin afters tests, will be called after each test is run.koinSetUp()
- Register a setUp function that starts koin before tests, will be called after each test is run.koinTest()
- Configures the testing environment to automatically start and close Koin for each test.
TIP
Don't hesitate to overload Koin modules configuration to help you partly build your app.
Test SetUp
Create a Koin context for your test
You can easily create and hold a Koin context for each of your test with the following setup:
note
koin_test package does not have mockito as a dependency.
Mocking out of the box
Instead of making a new module each time you need a mock, you can declare a mock on the fly with declare
:
Checking your Koin modules
Koin offers a way to test if you Koin modules are good: testModules
- walk through your definition tree and check if each definition is bound
Starting & stopping Koin for your tests
Take attention to stop your koin instance (if you use startKoin
in your tests) between every test. Else be sure to use koinApplication
, for local koin instances or stopKoin()
to stop the current global instance.