Flutter 에서 inheritedWidget으로 state 구현 시,
StatefulWidget의 State안에 있는 method의 unit test 구현
핵심은 아래 구문을 통해 State객체를 얻어올 수 있다는 것이다.
void main() {
group('AppStateContainer', () {
testWidgets('Test ranking', (WidgetTester tester) async {
await tester.pumpWidget(AppStateContainer(
child: new AppRootWidget('/'), state: new AppState()));
final AppStateContainerState appStateContainerState =
tester.state(find.byType(AppStateContainer));
appStateContainerState.state.singlePlayRecords = [];
for (int i = 1; i <= 10; i++) {
appStateContainerState.state.singlePlayRecords.add(new Record(
record: i * 0.001,
timeStamp: DateTime.now().millisecondsSinceEpoch));
}
expect(
appStateContainerState.getRankFromRecord(new Record(
record: 0.001, timeStamp: DateTime.now().millisecondsSinceEpoch)),
2);
expect(
appStateContainerState.getRankFromRecord(new Record(
record: 0.0001, timeStamp: DateTime.now().millisecondsSinceEpoch)),
1);
});
});
}
<참고> https://github.com/flutter/flutter/blob/master/packages/flutter/test/widgets/stateful_components_test.dart
댓글
댓글 쓰기