Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Write tests for Provider
Hiroki Matsue
May 22, 2019
Technology
5
510
Write tests for Provider
"Flutter Meetup Tokyo #9"でのLT資料です。
https://flutter-jp.connpass.com/event/126419/
Hiroki Matsue
May 22, 2019
Tweet
Share
More Decks by Hiroki Matsue
See All by Hiroki Matsue
matsue
2
320
matsue
3
610
matsue
2
530
matsue
2
1k
matsue
4
7.8k
matsue
0
2.9k
matsue
1
430
matsue
2
1.4k
matsue
0
550
Other Decks in Technology
See All in Technology
line_developers
PRO
0
2.3k
harshbothra
1
160
kekeke_47
0
430
shimacos
2
360
takuros
3
640
siroemk
0
260
kentaro
1
430
natsusan
0
200
nwiizo
1
160
gkzz
0
160
koba789
0
450
miura55
0
100
Featured
See All Featured
iamctodd
17
1.9k
kastner
54
1.9k
philhawksworth
192
8.8k
lara
590
61k
sachag
446
36k
malarkey
119
16k
davidbonilla
70
3.5k
holman
448
130k
brad_frost
156
6.4k
tammielis
237
23k
philnash
8
500
thoeni
4
550
Transcript
Write tests for Provider Hiroki Matsue (@macs_6) May 22th, 2019
Flutter Meetup Tokyo #9
This talk is about How to test Provider classes
What is Provider Google I/O 2019ͷFlutterʹ͓͚Δ࣮ફతͳstateཧͷൃදͰ հ͞Εͨύοέʔδ Pragmatic State Management
in Flutter (Google I/O'19) https://youtu.be/d_m5csmrf7I
A dependency injection system built with widgets for widgets. provider
is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward. https://github.com/rrousselGit/provider • ΄΅InheritedWidgetͷγϯλοΫεγϡΨʔ • BLoCΛ͏࣌ʹࣗલͰProvider૬ͷͷΛ࣮͍ͯͨ͠ ͣ
BLoC͚ʹॻ͍͍ͯͨInheritedWidgetΛͬͨProvider class CartProvider extends InheritedWidget { final CartBloc cartBloc; CartProvider({
Key key, CartBloc cartBloc, Widget child, }) : cartBloc = cartBloc ?? CartBloc(), super(key: key, child: child); @override bool updateShouldNotify(InheritedWidget oldWidget) => true; static CartBloc of(BuildContext context) => (context.inheritFromWidgetOfExactType(CartProvider) as CartProvider) .cartBloc; } final cartBloc = CartProvider.of(context); https://github.com/filiph/stateexperiments/blob/19c321bbc62ac10855751124e3ea9701e583d6ea/shared/lib/src/bloc/cartprovider.dart
ProviderύοέʔδΛ͏ͱ͜͏ͳΔ Provider<ExampleBloc>( builder: (_) => ExampleBloc(), dispose: (_, value) =>
value.dispose(), child: ExampleBloc(), ); final bloc = Provider.of<ExampleBloc>(context)
શ෦BLoCͰॻͭ͘ΓͳΒbloc_providerύοέʔδ bloc is disposed automatically https://pub.dev/packages/bloc_provider BlocProvider<ExampleBloc>( creator: (_context, _bag)
=> ExampleBloc(), child: Example(), )
ProviderΛ͏ͱ • streamRxDartΛΘͣʹෳWidgetؒͷstateΛཧͰ͖ ֶͯशίετ͕͍ • ඞཁʹԠͯ͡BLoCಋೖͰ͖Δ
How to write tests?
class Counter with ChangeNotifier { Counter( this._number, ); factory Counter.withInitialValues({
int number = 0, }) { return Counter(number); } int _number; int get number => _number; void increment() { _number++; notifyListeners(); } }
notifyListeners()Λແࢹ͢ΔͳΒ test('increment', () { final counter = Counter.withInitialValues(); expect(counter.number, 0);
counter.increment(); expect(counter.number, 1); });
֤छύοέʔδͷςετͰtestWidgetsΛ͍ͬͯΔ
UIςετͱͯ͠ widgetΛςετ͚ʹॳظԽ͢Δ
testWidgets("increment", (tester) async { final key = GlobalKey(); await tester.pumpWidget(
ChangeNotifierProvider( builder: (context) => Counter.withInitialValues(), child: MaterialApp( home: Consumer<Counter>( builder: (context, counter, child) => FlatButton( key: key, onPressed: () => counter.increment(), child: Text(counter.number.toString())), ), ), ), ); expect(find.text('0'), findsOneWidget); expect(find.text('1'), findsNothing); await tester.tap(find.byKey(key)); await tester.pumpAndSettle(); expect(find.text('0'), findsNothing); expect(find.text('1'), findsOneWidget); });
֤छύοέʔδͷςετͰݟΒΕΔ
͍ճ͢widgetΛอଘ͓ͯ͘͠ final tree = ChangeNotifierProvider( builder: (context) => Counter.withInitialValues(), child:
MaterialApp( home: Consumer<Counter>( builder: (context, counter, child) => FlatButton( key: key, onPressed: () => counter.increment(), child: Text(counter.number.toString())), ), ), );
ʹରͯ͠ݕূΛߦ͏ testWidgets("increment", (tester) async { final key = GlobalKey(); int
number; await tester.pumpWidget( ChangeNotifierProvider( builder: (context) => Counter.withInitialValues(), child: MaterialApp( home: Consumer<Counter>(builder: (context, counter, child) { number = counter.number; return FlatButton( key: key, onPressed: () => counter.increment(), child: Container(), ); }), ), ), ); });
notifierΛcontext͔Βऔಘͯ͠ݕূ͢Δ testWidgets('works with MultiProvider', (tester) async { final key =
GlobalKey(); var notifier = ChangeNotifier(); await tester.pumpWidget(MultiProvider( providers: [ ChangeNotifierProvider.value(notifier: notifier), ], child: Container(key: key), )); expect(Provider.of<ChangeNotifier>(key.currentContext), notifier); }); https://github.com/rrousselGit/provider/blob/fe778651565f7563dc4a1b2afb513119c5424761/test/changenotifierprovider_test.dart#L11-
·ͱΊ • testWidgets Λͬͯςετॻ͘ • ֤छύοέʔδΛݟΔͱςετͷهड़ΛݮΒ͕͢৭ʑݟ ΒΕΔ • ରͷwidgetΛ͍ճ͢ •
มʹೖΕͯݕূ͢Δ • notifierΛऔಘ͢Δ
References • Developer Questͷςετ: https://github.com/2d-inc/ developerquest/blob/master/test/worldtest.dart • Providerύοέʔδͷςετ: https://github.com/ rrousselGit/provider/blob/
fe778651565f7563dc4a1b2afb513119c5424761/test/ changenotifierprovider_test.dart • FlutterຊମͷChangeNotifierςετ: https://github.com/ flutter/flutter/blob/
Thanks