单元测试 依赖注入

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

使用依赖注入可以简化代码并使DRY保持干燥,还可以简化测试,甚至提高可测试性。

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

如果您尚未开始在应用程序中使用依赖项注入,则可能需要看一下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;
}

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.

这是我创建的一个小助手,用于各种UI的“从上到下”逻辑中,以跟踪最后更新的值,以确保在API数据未更改的情况下微调器消失。

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.

作为依赖注入的一部分,拥有一个简单的助手显然不是必需的,但是由于我们所有的BloC,Cubits,用例,存储库等已经与当前的代码样式保持一致。

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

如果在窗口小部件中使用,它将以以下方式访问:

sl<DateTimeHelpers>().nowInMilliseconds()

Where sl is a assigned toGetIt.instance.

其中sl是分配给GetIt.instance

For testing, you would simply use a mocked class:

为了进行测试,您只需使用一个模拟类:

class MockDateTimeHelpers extends Mock implements DateTimeHelpers {}

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.

现在,如果您在说一个块时使用了它,那么您就不需要模拟依赖项注入,您只需将MockClass提供给测试BLoC,但是,如果要在Widget中使用它,则对于该特定对象不太可能助手,您需要在测试代码中添加一些额外的行。

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;
});

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.

在测试的setUp方法中,您将初始化依赖关系,然后在其下启用allowReassignment 。 顾名思义,这可以让您重新分配依赖项。

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

然后,您可以使用模拟类注册新的依赖项:

mockDateTimeHelpers = MockDateTimeHelpers();sl.registerSingleton<DateTimeHelpers>(mockDateTimeHelpers);

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.

这将确保你的代码运行,如果您需要在您的测试,以控制返回的值,你可以简单地使用whenMokito来回报您选择的值。

when(mockDateTimeHelpers.nowInMilliseconds).thenReturn(100);

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.

然后,对于该一个或多个测试,取决于小部件的添加位置,则可以期望在Widget使用该帮助程序时返回100。

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 WyzetalkFounded 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.

关于Wyzetalk Wyzetalk 成立于南非,总部位于荷兰,是一家领先的全球员工体验公司,提供移动优先的数字解决方案,将大型组织与其分散的前线员工联系在一起,以改善沟通,释放创新并提高业务绩效。 自2012年成立以来,公司的收入每年以100%以上的速度增长。 Wyzetalk平台遍及五大洲的18个国家,目前在采矿,零售,快速消费品,制造,能源,汽车和航运等领域的客户中有650 000名员工。

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

网站: https //www.wyzetalk.com/



翻译自: https://medium.com/wyzetalk-tech/dependency-injection-and-testing-9356069706c8

单元测试 依赖注入