Skip to content

Commit

Permalink
Merge pull request #497 from watcher6280/448_Fix_Flaky_Test
Browse files Browse the repository at this point in the history
448 fix flaky test
  • Loading branch information
rolandgeider authored Jan 23, 2024
2 parents 5c2f40a + 143f735 commit 9f4b7e3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Abhishek Saini - <https://github.com/Abhisheksainii>
- Hanaa Allohibi - <https://github.com/hn-n>
- Shey Alnasrawi - <https://github.com/Milksheyke>
- Costas Korai - <https://github.com/watcher6280>

## Translators

Expand Down
3 changes: 2 additions & 1 deletion lib/models/nutrition/meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:wger/helpers/json.dart';
Expand Down Expand Up @@ -54,7 +55,7 @@ class Meal {

this.mealItems = mealItems ?? [];

this.time = time ?? TimeOfDay.now();
this.time = time ?? TimeOfDay.fromDateTime(clock.now());
this.name = name ?? '';
}

Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies:
flex_seed_scheme: ^1.4.0
flex_color_scheme: ^7.3.1
freezed_annotation: ^2.4.1
clock: ^1.1.1

dev_dependencies:
flutter_test:
Expand Down
52 changes: 35 additions & 17 deletions test/nutrition/nutritional_meal_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

var plan1 = NutritionalPlan.empty();
var meal1 = Meal();
final meal2 = Meal();

when(mockNutrition.editMeal(any)).thenAnswer((_) => Future.value(Meal()));
when(mockNutrition.addMeal(any, any)).thenAnswer((_) => Future.value(Meal()));
Expand Down Expand Up @@ -105,22 +105,40 @@ void main() {
});

testWidgets('Test creating a new nutritional plan', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen(meal2));
await tester.pumpAndSettle();

expect(
find.text(timeToString(TimeOfDay.now())!),
findsOneWidget,
reason: 'Current time is filled in',
);

await tester.enterText(find.byKey(const Key('field-time')), '08:00');
await tester.enterText(find.byKey(const Key('field-name')), 'test meal');
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));

// Correct method was called
verifyNever(mockNutrition.editMeal(any));
verify(mockNutrition.addMeal(any, any));
// DateTime.now() is difficult to mock as it pull directly from the platform
// currently being run. The clock pacakge (https://pub.dev/packages/clock)
// addresses this issue, using clock.now() allows us to set a fixed value as the
// response when using withClock.
final fixedDateTimeValue = DateTime(2020, 09, 01, 01, 01);
withClock(Clock.fixed(fixedDateTimeValue), () async {
// The time set in the meal object is what is displayed by default
// and can be matched with the find.text function. By creating the meal
// wrapped in the withClock it also shares the same now value.
final fixedTimeMeal = Meal();

await tester.pumpWidget(createHomeScreen(fixedTimeMeal));
await tester.pumpAndSettle();

expect(
clock.now(),
fixedDateTimeValue,
reason: 'Current time is set to a fixed value for testing',
);

expect(
find.text(timeToString(TimeOfDay.fromDateTime(clock.now()))!),
findsOneWidget,
reason: 'Current time is filled in',
);

await tester.enterText(find.byKey(const Key('field-time')), '08:00');
await tester.enterText(find.byKey(const Key('field-name')), 'test meal');
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));

// Correct method was called
verifyNever(mockNutrition.editMeal(any));
verify(mockNutrition.addMeal(any, any));
});

// Detail page
// ...
Expand Down

0 comments on commit 9f4b7e3

Please sign in to comment.