DEV Community

Reme Le Hane
Reme Le Hane

Posted on • Originally published at Medium on

Dependency Injection and Testing

Using dependency injection can be great for simplifying your code and keeping it DRY, it can also simplify testing and even improve testability.

If you have not started using dependency injection in your app, you may want to take a look at get_it.

For those already using it, this is how you would go about testing your code that implements these injections.

class DateTimeHelpers {
 int nowInMilliseconds() => DateTime.now().millisecondsSinceEpoch;
}
Enter fullscreen mode Exit fullscreen mode

That is a small helper I created for use within various UI’s pull-to-refresh logic to track a last updated value to ensure the spinner goes away incase the API data has not changed.

Obviously not essential to have a simple helper as part of the dependency injection but as all our BloC’s, Cubits, Use Cases, Repositories, etc already is it just keeps in line with the current code-styling.

If used in a widget, it would be accessed as:

sl<DateTimeHelpers>().nowInMilliseconds()
Enter fullscreen mode Exit fullscreen mode

Where sl is assigned to GetIt.instance.

For testing, you would simply use a mocked class:

class MockDateTimeHelpers extends Mock implements DateTimeHelpers {}
Enter fullscreen mode Exit fullscreen mode

Now if you were using this in say a bloc you would not need to also mock the dependency injection, you would simply provide the MockClass to your test BLoC, however, if you were to use this in a Widget which would be unlikely for this specific helper, you would need to add a few extra lines to your testing code.

You would need to initialize your dependencies within your test, same as you would have done in your application code.

setUp(() {
  // Setup the application services
  di.initServices();
  di.sl.allowReassignment = true;
});
Enter fullscreen mode Exit fullscreen mode

Within your tests setUp method, you would initialize your dependencies and then beneath that you enable allowReassignment. That, as the name suggests allows you to reassign your dependencies.

You can then register a new dependency using your mocked class:

mockDateTimeHelpers = MockDateTimeHelpers();

sl.registerSingleton<DateTimeHelpers>(mockDateTimeHelpers);
Enter fullscreen mode Exit fullscreen mode

This will ensure your code runs, and if you need to control the returned value during your tests you can simply use the when from Mokito to return a value of your choosing.

when(mockDateTimeHelpers.nowInMilliseconds).thenReturn(100);
Enter fullscreen mode Exit fullscreen mode

Then for that test or tests, depending on where you added that you can then expect that 100 to be returned whenever the Widget uses that helper.

I hope you found this useful, and if you have any questions, comments, or improvements, feel free to drop me a comment.

Thanks for reading.

About Wyzetalk

Founded in South Africa and headquartered in The Netherlands, Wyzetalk is a leading global employee experience company that offers a mobile-first digital solution connecting large organisations with their dispersed, frontline workforce to improve communication, unleash innovation, and boost business performance. Since launching in 2012, the company has grown in revenue by more than 100% per annum. With a presence in 18 countries across five continents, today there are 650 000 employees making use of the Wyzetalk platform through clients in the Mining, Retail, FMCG, Manufacturing, Energy, Automotive and Shipping sectors.

Website: https://www.wyzetalk.com/

Top comments (0)