-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from samuelematias/lesson20
Lesson 20: Stubbing asynchronous methods using mockito
- Loading branch information
Showing
7 changed files
with
229 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:calculator/calculator.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class Pi extends StatelessWidget { | ||
Pi({Key key, @required Calculator calculator}) | ||
: assert(calculator != null), | ||
_calculator = calculator, | ||
super(key: key); | ||
|
||
final Calculator _calculator; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
String _result; | ||
|
||
return StreamBuilder<double>( | ||
stream: _calculator.pi(), | ||
initialData: null, | ||
builder: (context, snapshot) { | ||
if (snapshot.data != null) { | ||
_result = snapshot.data.toString(); | ||
} | ||
return Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Text( | ||
_result != null | ||
? 'The latest known value of pi is $_result' | ||
: 'Calculating pi...', | ||
style: Theme.of(context).textTheme.headline5, | ||
), | ||
], | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:calculator/calculator.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class PowerOfTwo extends StatefulWidget { | ||
const PowerOfTwo({ | ||
Key key, | ||
@required Calculator calculator, | ||
}) : assert(calculator != null), | ||
_calculator = calculator, | ||
super(key: key); | ||
|
||
final Calculator _calculator; | ||
|
||
@override | ||
State<StatefulWidget> createState() => _PowerOfTwoState(); | ||
} | ||
|
||
class _PowerOfTwoState extends State<PowerOfTwo> { | ||
final _controller = TextEditingController(); | ||
|
||
String _result; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller.addListener(_getResult); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
TextFormField( | ||
key: const Key('textField_powerOfTwo'), | ||
controller: _controller, | ||
keyboardType: TextInputType.number, | ||
), | ||
Text( | ||
'to the power of two', | ||
style: Theme.of(context).textTheme.headline5, | ||
), | ||
Text( | ||
'is ${_result ?? '???'}', | ||
style: Theme.of(context).textTheme.headline5, | ||
), | ||
], | ||
); | ||
} | ||
|
||
void _getResult() async { | ||
try { | ||
final input = double.tryParse(_controller.text); | ||
final powerOfTwoResult = await widget._calculator.powerOfTwo(input); | ||
setState(() { | ||
_result = powerOfTwoResult.toString(); | ||
}); | ||
} catch (_) { | ||
setState(() { | ||
_result = null; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:calculator/calculator.dart'; | ||
import 'package:calculator_app/pi.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
class MockCalculator extends Mock implements Calculator {} | ||
|
||
void main() { | ||
Calculator calculator; | ||
|
||
setUp(() { | ||
calculator = MockCalculator(); | ||
}); | ||
|
||
group('Pi', () { | ||
testWidgets('renders the result provided by the calculator', | ||
(tester) async { | ||
when(calculator.pi()).thenAnswer((realInvocation) => Stream.periodic( | ||
Duration(milliseconds: 400), | ||
(eventIndex) { | ||
if (eventIndex == 0) return 3.1; | ||
if (eventIndex == 1) return 3.14; | ||
return null; | ||
}, | ||
)); | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: Pi( | ||
calculator: calculator, | ||
), | ||
), | ||
), | ||
); | ||
expect(find.text('Calculating pi...'), findsOneWidget); | ||
await tester.pumpAndSettle(Duration(milliseconds: 400)); | ||
expect( | ||
find.text('The latest known value of pi is 3.1'), | ||
findsOneWidget, | ||
); | ||
await tester.pumpAndSettle(Duration(milliseconds: 400)); | ||
expect( | ||
find.text('The latest known value of pi is 3.14'), | ||
findsOneWidget, | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:calculator/calculator.dart'; | ||
import 'package:calculator_app/power_of_two.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
class MockCalculator extends Mock implements Calculator {} | ||
|
||
void main() { | ||
Calculator calculator; | ||
|
||
setUp(() { | ||
calculator = MockCalculator(); | ||
}); | ||
|
||
group('PowerOfTwo', () { | ||
testWidgets('renders the result provided by the calculator', | ||
(tester) async { | ||
when(calculator.powerOfTwo(5)) | ||
.thenAnswer((realInvocation) => Future.value(100)); | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: PowerOfTwo( | ||
calculator: calculator, | ||
), | ||
), | ||
), | ||
); | ||
await tester.enterText(find.byKey(Key('textField_powerOfTwo')), '5'); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('is 100.0'), findsOneWidget); | ||
}); | ||
}); | ||
} |