Test

Getting Started with Test

This tutorial lets you test a Dart application and use Koin inject and retrieve your components.

Setup

First, add the Koin dependency like below:

PackagePub
koinpub package
koin_testpub package
dependencies:
koin: ^[laste_version]
dev_dependencies:
koin_test: ^[laste_version]

Declared dependencies

We reuse the koin dart getting started example, to use the koin module:

final helloModule = Module()
..single((s) => HelloMessageData())
..single<HelloService>((s) => HelloServiceImpl(s.get()))

Writing our first Test

To make our first test, let's write a simple unit test file and import koin_test.dart. We will be able then, to use by inject() operators.

void main() {
setUp(() {
startKoin((app) {
app.module(helloModule);
});
});
tearDown(() {
stopKoin();
});
var model = get<HelloMessageData>();
var service = get<HelloService>();
test('unit test', () {
var helloApp = HelloApplication();
helloApp.sayHello();
expect(service, helloApp.helloService);
expect("Hey, ${model.message}", service.hello());
});
}

Mocking

You can use the declare function to declare mocks:

HelloMessageDataMock implements HelloMessageData {}
void main() {
setUp(() {
startKoin((app) {
app.module(helloModule);
});
});
tearDown((){
stopKoin();
});
test('should inject my components', () {
var dataMock = HelloMessageDataMock();
// declare a mock instance to HelloMessageData.
declare<HelloMessageData>(dataMock);
// retrieve mock, same as variable above
expect(get<HelloMessageData>(), isA<HelloMessageDataMock>());
// then HelloService will be using the mocked instance.
expect(get<HelloService>().helloMessageData, isA<HelloMessageDataMock>());
});
}

Verifying modules

Let's write our check test as follow:

  • test modules with testModule() function
void main(){
testModule('moduleChecktest - shoud be a valid module',helloModule);
}