Skip to content

Commit 135ddb4

Browse files
Costas KoraiCostas Korai
authored andcommitted
Update to use clock.now where needed
- The initial time set in the MealForm is defined in the meal model object provided to the MealForm so both the test and the meal object had to use clock.now in order for the fixed value to apply to both.
1 parent 4f2ee9e commit 135ddb4

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

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

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)