我有一个CustomTabBar
返回 TabBarWidget 的类。所以CustomTabBar
小部件的基本结构如下:
class CustomTabBar extends StatelessWidget {
...
final bool isBottomIndicator;
...
@override
Widget build(BuildContext context) {
return TabBar(
.....
indicator: BoxDecoration(
border: isBottomIndicator
? Border(
bottom: BorderSide(
color: Theme.of(context).colorScheme.tabbarSelectorColor,
width: 3.0,
),
)
: Border(
top: BorderSide(
color: Theme.of(context).colorScheme.tabbarSelectorColor,
width: 3.0,
),
),
),
......
);
}
}
指示器的位置由变量控制isBottomIndicator
。
现在我正在尝试为这个小部件编写一个测试用例。到目前为止,我已经尝试过这个测试用例:
testWidgets('CustomTabBar with bottom indicator',
(WidgetTester tester) async {
final _customTabBar = CustomTabBar(
tabs: _tabs,
selectedIndex: _selectedIndex,
onTap: (_) {},
isBottomIndicator: true,
);
await tester.pumpWidget(makeTestableWidgets(child: _customTabBar));
final tabBar = find.byType(TabBar).first as TabBar;
expect(
tabBar.indicator,
BoxDecoration(
border: Border(
bottom: BorderSide(
width: 3.0,
),
),
),
);
});
我从编译器得到的例外是
The following _CastError was thrown running a test:
type '_WidgetTypeFinder' is not a subtype of type 'TabBar' in type cast
我不知道我怎样才能通过我找到的 wigetfind.byType()
以下测试通过了我检查 TabBar 是否存在的地方。
testWidgets('CustomTabBar has a TabBar', (WidgetTester tester) async {
await tester.pumpWidget(makeTestableWidgets(child: _customTabBar));
expect(find.byType(TabBar), findsOneWidget);
});
如果您对makeTestableWidgets
. 这个函数只是为 TabBar 提供 Material App。
Widget makeTestableWidgets({Widget child}) {
return MaterialApp(
home: DefaultTabController(
length: 1,
child: Scaffold(
body: Container(),
bottomNavigationBar: _customTabBar,
),
),
);
}