cpd/test/unit_testing/habitcounter_test.dart

58 lines
2.3 KiB
Dart

import 'package:cpd/database/db_interface.dart';
import 'package:cpd/database/todo_interface.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:cpd/database/habit.dart';
import 'package:cpd/pages/homepage.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'habitcounter_test.mocks.dart';
import 'home_page_test.mocks.dart'; // Import der generierten Mocks
@GenerateMocks([HabitDbInterface, ToDoInterface])
void main() {
group('MyHomePage', () {
test('habitcounter returns correct count', () {
List<Habit> habits = [
Habit(id: 1, title: 'Habit 1', subtitle: 'Description 1', icon: Icons.star, isComplete: false),
Habit(id: 2, title: 'Habit 2', subtitle: 'Description 2', icon: Icons.star_border, isComplete: true),
Habit(id: 3, title: 'Habit 3', subtitle: 'Description 3', icon: Icons.favorite, isComplete: true),
];
final myHomePageState = MyHomePageState();
final habitCount = myHomePageState.countHabits(habits);
expect(habitCount, 3);
});
testWidgets('displays correct habit count text', (WidgetTester tester) async {
final mockHabitDbInterface = MockHabitDbInterface();
final mockToDoInterface = MockToDoInterface();
List<Habit> habits = [
Habit(id: 1, title: 'Habit 1', subtitle: 'Description 1', icon: Icons.star, isComplete: false),
Habit(id: 2, title: 'Habit 2', subtitle: 'Description 2', icon: Icons.star_border, isComplete: true),
Habit(id: 3, title: 'Habit 3', subtitle: 'Description 3', icon: Icons.favorite, isComplete: true),
];
// Hier mocken wir die fetchAll Methode des mockToDoInterface
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
// Stellen Sie sicher, dass der MyHomePage Konstruktor die mockToDoInterface Instanz erhält
await tester.pumpWidget(MaterialApp(
home: MyHomePage(database: mockHabitDbInterface, title: 'homepage'),
));
// Act
await tester.pumpAndSettle(); // Warten bis die Widget-Tree sich beruhigt
// Assert
expect(find.text('You have 3 habits for today'), findsOneWidget);
verify(mockToDoInterface.fetchAll()).called(1);
verifyNoMoreInteractions(mockToDoInterface);
});
});
}