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 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'habitcounter_test.mocks.dart'; @GenerateMocks([HabitDbInterface, ToDoInterface]) void main() { setUpAll(() { // Initialisierung von sqflite_ffi sqfliteFfiInit(); databaseFactory = databaseFactoryFfi; }); group('MyHomePage', () { test('habitcounter returns correct count', () { List 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 mockToDoInterface = MockToDoInterface(); List 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), ]; // Mocking the fetchAll method of mockToDoInterface when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits); // Pumpe das Widget mit MaterialApp um den FutureBuilder zu umgeben await tester.pumpWidget( MaterialApp( home: FutureBuilder>( future: mockToDoInterface.fetchAll(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) { // Data is available, return a widget with the correct text return Text('You have ${snapshot.data!.length} habits for today'); } else { // Data is not available or in loading state, return a widget with 'No habits' text return const Text('No habits'); } }, ), ), ); await tester.pumpAndSettle(); // Überprüfe, ob der Text "You have x habits for today" angezeigt wird expect(find.text('You have ${habits.length} habits for today'), findsOneWidget); // Überprüfe, ob der Text "No habits" angezeigt wird, wenn keine Daten verfügbar sind expect(find.text('No habits'), findsNothing); verify(mockToDoInterface.fetchAll()).called(1); verifyNoMoreInteractions(mockToDoInterface); }); }); }