Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 19: Stubbing synchronous methods with mockito #19

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Calculator - [Caster.IO](https://caster.io) project to showcase testing techniqu

[Lesson 18: Controlling the behavior of your dependencies using mockito](https://caster.io/lessons/lesson-18-controlling-the-behavior-of-your-dependencies-using-mockito) 👉🏾 [Branch](https://github.com/samuelematias/calculator_app/tree/lesson18)

[Lesson 19: Stubbing synchronous methods with mock](https://caster.io/lessons/lesson-19-stubbing-synchronous-methods-with-mockito) 👉🏾 [Branch](https://github.com/samuelematias/calculator_app/tree/lesson19)

## License

```
Expand Down
34 changes: 31 additions & 3 deletions test/two_digit_operation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ void main() {

group('TwoDigitOperation', () {
group('Operation.add', () {
testWidgets('paints 4.0 when adding 3 and 1', (tester) async {
when(calculator.add(3, 1)).thenReturn(4);
testWidgets('paints the value returned by the calculator',
(tester) async {
when(calculator.add(3, 1)).thenReturn(25);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
Expand All @@ -37,7 +38,34 @@ void main() {
'1',
);
await tester.pumpAndSettle();
expect(find.text('is 4.0'), findsOneWidget);
expect(find.text('is 25.0'), findsOneWidget);
});
});

group('Operation.divide', () {
testWidgets('paints the value returned by the calculator',
(tester) async {
when(calculator.divide(3, 0)).thenThrow(Exception('oops'));
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: TwoDigitOperation(
operation: Operations.divide,
calculator: calculator,
),
),
),
);
await tester.enterText(
find.byKey(const Key('textField_top_divided by')),
'3',
);
await tester.enterText(
find.byKey(const Key('textField_bottom_divided by')),
'0',
);
await tester.pumpAndSettle();
expect(find.text('is ???'), findsOneWidget);
});
});
});
Expand Down