CPD/test/widget_test.dart

92 lines
4.0 KiB
Dart
Raw Normal View History

2024-06-19 15:14:06 +02:00
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:momo/main.dart';
void main() {
testWidgets('Kalorienverbrauch Rechner - Valid Input', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
// Find text fields and button
final gewichtField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Gewicht (in kg)');
final streckeField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Strecke (in km)');
final zeitField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Zeit (in Minuten)');
final berechnenButton = find.byType(ElevatedButton);
// Enter valid input
await tester.enterText(gewichtField, '70');
await tester.enterText(streckeField, '10');
await tester.enterText(zeitField, '60');
// Tap the calculate button
await tester.tap(berechnenButton);
// Rebuild the widget after the state has changed
await tester.pump();
// Verify that the calculation is correct
expect(find.text('Geschwindigkeit pro Kilometer: 6.00 Minuten'), findsOneWidget);
expect(find.text('Kalorienverbrauch: 700.00 kcal'), findsOneWidget);
});
testWidgets('Kalorienverbrauch Rechner - Invalid Input', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
// Find text fields and button
final gewichtField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Gewicht (in kg)');
final streckeField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Strecke (in km)');
final zeitField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Zeit (in Minuten)');
final berechnenButton = find.byType(ElevatedButton);
// Enter invalid input
await tester.enterText(gewichtField, '70');
await tester.enterText(streckeField, '10');
await tester.enterText(zeitField, '30'); // Invalid speed
// Tap the calculate button
await tester.tap(berechnenButton);
// Rebuild the widget after the state has changed
await tester.pump();
// Verify that the error message is displayed
expect(find.text('Ungültige Angaben'), findsOneWidget);
});
testWidgets('Kalorienverbrauch Rechner - Edge Case Input', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
// Find text fields and button
final gewichtField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Gewicht (in kg)');
final streckeField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Strecke (in km)');
final zeitField = find.byWidgetPredicate((widget) => widget is TextField && widget.decoration?.labelText == 'Zeit (in Minuten)');
final berechnenButton = find.byType(ElevatedButton);
// Enter edge case input (speed = 15 km/h)
await tester.enterText(gewichtField, '70');
await tester.enterText(streckeField, '15');
await tester.enterText(zeitField, '60');
// Tap the calculate button
await tester.tap(berechnenButton);
// Rebuild the widget after the state has changed
await tester.pump();
// Debug output
print(tester.widgetList(find.text('Geschwindigkeit pro Kilometer:')).toList());
print(tester.widgetList(find.text('Kalorienverbrauch:')).toList());
// Verify that the calculation is correct
expect(find.text('Geschwindigkeit pro Kilometer: 4.00 Minuten'), findsOneWidget);
expect(find.text('Kalorienverbrauch: 840.00 kcal'), findsOneWidget);
});
}