Skip to content

Commit 9f4b7e3

Browse files
authored
Merge pull request #497 from watcher6280/448_Fix_Flaky_Test
448 fix flaky test
2 parents 5c2f40a + 143f735 commit 9f4b7e3

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Abhishek Saini - <https://github.com/Abhisheksainii>
2626
- Hanaa Allohibi - <https://github.com/hn-n>
2727
- Shey Alnasrawi - <https://github.com/Milksheyke>
28+
- Costas Korai - <https://github.com/watcher6280>
2829

2930
## Translators
3031

lib/models/nutrition/meal.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import 'package:clock/clock.dart';
1920
import 'package:flutter/material.dart';
2021
import 'package:json_annotation/json_annotation.dart';
2122
import 'package:wger/helpers/json.dart';
@@ -54,7 +55,7 @@ class Meal {
5455

5556
this.mealItems = mealItems ?? [];
5657

57-
this.time = time ?? TimeOfDay.now();
58+
this.time = time ?? TimeOfDay.fromDateTime(clock.now());
5859
this.name = name ?? '';
5960
}
6061

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dependencies:
6767
flex_seed_scheme: ^1.4.0
6868
flex_color_scheme: ^7.3.1
6969
freezed_annotation: ^2.4.1
70+
clock: ^1.1.1
7071

7172
dev_dependencies:
7273
flutter_test:

test/nutrition/nutritional_meal_form_test.dart

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import 'package:clock/clock.dart';
1920
import 'package:flutter/material.dart';
2021
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2122
import 'package:flutter_test/flutter_test.dart';
@@ -39,7 +40,6 @@ void main() {
3940

4041
var plan1 = NutritionalPlan.empty();
4142
var meal1 = Meal();
42-
final meal2 = Meal();
4343

4444
when(mockNutrition.editMeal(any)).thenAnswer((_) => Future.value(Meal()));
4545
when(mockNutrition.addMeal(any, any)).thenAnswer((_) => Future.value(Meal()));
@@ -105,22 +105,40 @@ void main() {
105105
});
106106

107107
testWidgets('Test creating a new nutritional plan', (WidgetTester tester) async {
108-
await tester.pumpWidget(createHomeScreen(meal2));
109-
await tester.pumpAndSettle();
110-
111-
expect(
112-
find.text(timeToString(TimeOfDay.now())!),
113-
findsOneWidget,
114-
reason: 'Current time is filled in',
115-
);
116-
117-
await tester.enterText(find.byKey(const Key('field-time')), '08:00');
118-
await tester.enterText(find.byKey(const Key('field-name')), 'test meal');
119-
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
120-
121-
// Correct method was called
122-
verifyNever(mockNutrition.editMeal(any));
123-
verify(mockNutrition.addMeal(any, any));
108+
// DateTime.now() is difficult to mock as it pull directly from the platform
109+
// currently being run. The clock pacakge (https://pub.dev/packages/clock)
110+
// addresses this issue, using clock.now() allows us to set a fixed value as the
111+
// response when using withClock.
112+
final fixedDateTimeValue = DateTime(2020, 09, 01, 01, 01);
113+
withClock(Clock.fixed(fixedDateTimeValue), () async {
114+
// The time set in the meal object is what is displayed by default
115+
// and can be matched with the find.text function. By creating the meal
116+
// wrapped in the withClock it also shares the same now value.
117+
final fixedTimeMeal = Meal();
118+
119+
await tester.pumpWidget(createHomeScreen(fixedTimeMeal));
120+
await tester.pumpAndSettle();
121+
122+
expect(
123+
clock.now(),
124+
fixedDateTimeValue,
125+
reason: 'Current time is set to a fixed value for testing',
126+
);
127+
128+
expect(
129+
find.text(timeToString(TimeOfDay.fromDateTime(clock.now()))!),
130+
findsOneWidget,
131+
reason: 'Current time is filled in',
132+
);
133+
134+
await tester.enterText(find.byKey(const Key('field-time')), '08:00');
135+
await tester.enterText(find.byKey(const Key('field-name')), 'test meal');
136+
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
137+
138+
// Correct method was called
139+
verifyNever(mockNutrition.editMeal(any));
140+
verify(mockNutrition.addMeal(any, any));
141+
});
124142

125143
// Detail page
126144
// ...

0 commit comments

Comments
 (0)