自定义复选框上的 Flutter 小部件测试
Flutter widget test on custom checkbox
我有一个像这样的自定义复选框:
FeedbackCheckBox(
title: 'Test',
onChanged: (value) {
setState(
() {
isNeedComeBack = value;
},
);
},
)
这些是实现:
class FeedbackCheckBox extends HookWidget {
const FeedbackCheckBox({
Key? key,
required this.title,
required this.onChanged,
this.textStyle,
this.padding,
}) : super(key: key);
final String title;
final TextStyle? textStyle;
final ValueChanged<bool> onChanged;
final EdgeInsets? padding;
@override
Widget build(BuildContext context) {
final isNeedComeBack = useState<bool>(false);
return Padding(
padding: padding ?? const EdgeInsets.only(top: 12),
child: CustomCheckbox(
value: isNeedComeBack.value,
label: title,
onTap: () {
isNeedComeBack.value = !isNeedComeBack.value;
onChanged(isNeedComeBack.value);
},
),
);
}
}
现在我想为它写一个小部件测试。我想在点击 CustomCheckbox
上的 onTap 时,onChanged: (value)
收到正确的值。
这是我的测试:
testWidgets("FeedbackCheckBox widget onChanged works",
(WidgetTester tester) async {
bool clicked = false;
_onTap(v) => clicked = v;
var mockWidget = FeedbackCheckBox(title: "hhh", onChanged: _onTap);
final mockCustomCheckboxWidget =
CustomCheckbox(value: false, onTap: () {});
await tester.pumpWidget(withMaterialApp(child: mockWidget));
var customCheckBox =
tester.firstWidget(find.byWidget(mockCustomCheckboxWidget));
// await tester.tap(inkWellFinder);
// await tester.pumpAndSettle();
// expect(clicked, true);
});
我找到了 customCheckBox
,但如何触发 onTap
并在 FeedbackCheckBox onChanged
上接收值?
我解决了这个问题,这是新测试:
testWidgets("FeedbackCheckBox widget onChanged works",
(WidgetTester tester) async {
bool clicked = true;
_onTap(v) {
return clicked = v;
}
var mockWidget = FeedbackCheckBox(title: "hhh", onChanged: _onTap);
await tester.pumpWidget(withMaterialApp(child: mockWidget));
await tester.tap(find.byType(CustomCheckbox));
await tester.tap(find.byType(CustomCheckbox));
expect(clicked, false);
});
});
}
我有一个像这样的自定义复选框:
FeedbackCheckBox(
title: 'Test',
onChanged: (value) {
setState(
() {
isNeedComeBack = value;
},
);
},
)
这些是实现:
class FeedbackCheckBox extends HookWidget {
const FeedbackCheckBox({
Key? key,
required this.title,
required this.onChanged,
this.textStyle,
this.padding,
}) : super(key: key);
final String title;
final TextStyle? textStyle;
final ValueChanged<bool> onChanged;
final EdgeInsets? padding;
@override
Widget build(BuildContext context) {
final isNeedComeBack = useState<bool>(false);
return Padding(
padding: padding ?? const EdgeInsets.only(top: 12),
child: CustomCheckbox(
value: isNeedComeBack.value,
label: title,
onTap: () {
isNeedComeBack.value = !isNeedComeBack.value;
onChanged(isNeedComeBack.value);
},
),
);
}
}
现在我想为它写一个小部件测试。我想在点击 CustomCheckbox
上的 onTap 时,onChanged: (value)
收到正确的值。
这是我的测试:
testWidgets("FeedbackCheckBox widget onChanged works",
(WidgetTester tester) async {
bool clicked = false;
_onTap(v) => clicked = v;
var mockWidget = FeedbackCheckBox(title: "hhh", onChanged: _onTap);
final mockCustomCheckboxWidget =
CustomCheckbox(value: false, onTap: () {});
await tester.pumpWidget(withMaterialApp(child: mockWidget));
var customCheckBox =
tester.firstWidget(find.byWidget(mockCustomCheckboxWidget));
// await tester.tap(inkWellFinder);
// await tester.pumpAndSettle();
// expect(clicked, true);
});
我找到了 customCheckBox
,但如何触发 onTap
并在 FeedbackCheckBox onChanged
上接收值?
我解决了这个问题,这是新测试:
testWidgets("FeedbackCheckBox widget onChanged works",
(WidgetTester tester) async {
bool clicked = true;
_onTap(v) {
return clicked = v;
}
var mockWidget = FeedbackCheckBox(title: "hhh", onChanged: _onTap);
await tester.pumpWidget(withMaterialApp(child: mockWidget));
await tester.tap(find.byType(CustomCheckbox));
await tester.tap(find.byType(CustomCheckbox));
expect(clicked, false);
});
});
}