我最近开始涉足Flutter,但就在我准备写一些小部件测试时,发现我不太确定如何模拟Google Maps Flutter包。
许多示例都包括使用库“mockito”来模拟类,但这假定Google Maps小部件将被注入到要测试的小部件中。不幸的是,在给定的文档和启动指南中,似乎很难实现这一点:
class MapsDemo extends StatefulWidget {
@override
State createState() => MapsDemoState();
}
class MapsDemoState extends State<MapsDemo> {
GoogleMapController mapController;
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(15.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(
child: SizedBox(
width: 300.0,
height: 200.0,
child: GoogleMap(
onMapCreated: _onMapCreated,
),
),
),
RaisedButton(
child: const Text('Go to London'),
onPressed: mapController == null ? null : () {
mapController.animateCamera(CameraUpdate.newCameraPosition(
const CameraPosition(
bearing: 270.0,
target: LatLng(51.5160895, -0.1294527),
tilt: 30.0,
zoom: 17.0,
),
));
},
),
],
),
);
}
void _onMapCreated(GoogleMapController controller) {
setState(() { mapController = controller; });
}
}
请注意,不能传入GoogleMaps小部件,因为onMapCreated是必需的函数,而该函数依赖于私有类方法(使父小部件访问GoogleMapsController)。许多其他mockito模拟函数没有这种回调函数来设置状态。
我没有看到任何其他可以有效模拟GoogleMaps小部件的软件包,所以我没有任何示例可供参考。理想情况下,我期望像node.s中的proxyquire或sinon一样的行为(您不需要将模拟库传递到function.constructors中),但看起来,需要将mockified类传递到被测试的小部件中。
有没有其他想法可以模拟此库进行测试?还是我应该接受测试实际功能的事实?