我有以下代码
Widget _mapContainer(BuildContext context, bool isMapGesturesEnabled) {
var coordinates = LatLng(0, 0);
var state = context.bloc<AuthenticationCubit>().state;
// ignore: todo
// TODO: Improve how props are handled
var user = (state.props[0] as User);
var factoryName = user.factoryName;
if (factoryName != null) {
var factories = context.bloc<ApiCubit>().fetchFactories();
var userFactory = factories.firstWhere(
(Factory userFactory) => userFactory.name == factoryName);
coordinates = userFactory.coordinates;
}
}
我的意图是使用Mockito模拟这一部分
var state = context.bloc<AuthenticationCubit>().state;
,以便显示状态AuthenticationSuccess
有没有一种快速的方法可以为小部件测试设置模拟?
当前我有以下测试,但失败了,因为其中的state值为null
class _MockAuthenticationCubit extends Mock implements AuthenticationCubit {}
void main() {
group('Dashboard', () {
_createDashboard() {
return MultiBlocProvider(
providers: [
BlocProvider<AuthenticationCubit>(
create: (context) => _MockAuthenticationCubit(),
),
],
child: MediaQuery(
data: MediaQueryData(),
child: MaterialApp(
localizationsDelegates: [
S.delegate,
],
supportedLocales: S.delegate.supportedLocales,
home: Dashboard(),
),
),
);
}
group('MapLegend', () {
testWidgets('should display a map',
(WidgetTester tester) async {
User user = User(Role.MANAGER, "my_factory");
await tester.pumpWidget(_createBoard());
when(_MockAuthenticationCubit().state)
.thenReturn(AuthenticationSuccess(user: user));
await tester.pump();
expect(
find.byType(MapLegend),
findsOneWidget,
);
});
});
});
}