我一直在学习 Flutter/Dart 和 BLoC 模式。我以这篇文章为起点: https ://www.didierboelens.com/2018/08/reactive-programming---streams---bloc/
我有 bloc 类和小部件工作,但我不知道如何测试小部件。我正在使用BlocProvider
文章中描述的 a ,但我不知道如何为小部件提供模拟的 bloc 类。
如果我有这样的代码:
@override
Widget build(BuildContext context) {
final ProfileBloc profileBloc = BlocProvider.of<ProfileBloc>(context);
return Scaffold(
body: Container(
child: StreamBuilder<AuthModel>(
stream: profileBloc.outAuthModel,
initialData: null,
builder: (BuildContext context, AsyncSnapshot<AuthModel> snapshot) {
if (snapshot.hasData) {
return buildProfilePage(context, snapshot.data.profile);
}
return buildStartPage();
},
),
));
}
我想模拟我的 ProfileBloc,但它是在我的 build() 函数中创建的,并且需要上下文。如何测试这个小部件?我想我需要一种方法来传递一个模拟的 ProfileBloc,但我不知道该怎么做。我想确保小部件按预期运行。