【发布时间】:2021-09-21 11:56:17
【问题描述】:
所以我有一个简单的计数器应用程序,
class CounterApp extends StatefulWidget {
const CounterApp({Key? key}) : super(key: key);
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Text(_counter.toString()),
),
);
}
}
那么我该如何测试_counter 状态呢?
我试过这样做,
testWidgets("counter", (tester) async {
const key = Key("counter");
await tester.pumpWidget(const CounterApp(key: key));
final state = tester.state(find.byKey(key));
expect(state._counter, 0);
});
但我收到错误 Error: The getter '_counter' isn't defined for the class。我们甚至应该测试状态吗?
【问题讨论】:
标签: flutter flutter-test