16
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
*/
18
18
19
+ import 'package:clock/clock.dart' ;
19
20
import 'package:flutter/material.dart' ;
20
21
import 'package:flutter_gen/gen_l10n/app_localizations.dart' ;
21
22
import 'package:flutter_test/flutter_test.dart' ;
@@ -39,7 +40,6 @@ void main() {
39
40
40
41
var plan1 = NutritionalPlan .empty ();
41
42
var meal1 = Meal ();
42
- final meal2 = Meal ();
43
43
44
44
when (mockNutrition.editMeal (any)).thenAnswer ((_) => Future .value (Meal ()));
45
45
when (mockNutrition.addMeal (any, any)).thenAnswer ((_) => Future .value (Meal ()));
@@ -105,22 +105,40 @@ void main() {
105
105
});
106
106
107
107
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
+ });
124
142
125
143
// Detail page
126
144
// ...
0 commit comments