jj.*_*jj. 5 unit-testing flutter rxdart
我一直在学习 Flutter/Dart 和 BLoC 模式。我以这篇文章为起点:https : //www.didierboelens.com/2018/08/reactive-programming---streams---bloc/
我有 bloc 类和小部件工作,但我不知道如何测试小部件。我正在使用BlocProvider文章中描述的 ,但我不知道如何为小部件提供模拟的 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();
},
),
));
}
Run Code Online (Sandbox Code Playgroud)
我想模拟我的 ProfileBloc,但它是在我的 build() 函数中创建的,并且需要上下文。我如何测试这个小部件?我想我需要一种方法来传入一个模拟的 ProfileBloc,但我不知道该怎么做。我想确保小部件按预期运行。
在此示例中,您使用BlocProvider来获取ProfileBloc,但您可以直接使用 来创建一个新块final ProfileBloc profileBloc = ProfileBloc;。使用外部块应该不重要,因为这是在小部件测试之后。
| 归档时间: |
|
| 查看次数: |
2247 次 |
| 最近记录: |