首页 > 解决方案 > 如何在不明确传递所有模拟类的情况下对颤振进行单元测试?

问题描述

我试图找到一种方法来模拟一个类,而无需通过 Providers 将其模拟版本作为参数显式传递。我刚开始使用 Geolocator,我想模拟它的几个功能,例如getCurrentPosition.

到目前为止,我已经通过 Providers 传递了单例,以便在测试应用程序时可以模拟它们。但是 Geolocator 是一个静态类,当我尝试它提供一个类型时,它会说:

The return type 'Type' isn't a 'Geolocator', as required by the closure's context.

代码如下所示:

Provider<Geolocator>(create: (context) => Geolocator)

最干净的解决方案似乎能够注入/提供该类而无需使用Provider. 但是,我也无法通过MockGeolocator。所以我该怎么做?

标签: unit-testingfluttermockito

解决方案


推荐阅读