created all objects in setup() funktion to avoid redundant withClock

This commit is contained in:
2025-11-19 15:02:32 +01:00
parent 8150b42dba
commit c76e193b4d
3 changed files with 79 additions and 92 deletions

View File

@@ -8,6 +8,7 @@ import 'package:game_tracker/data/dto/player.dart';
void main() {
late AppDatabase database;
late Player testPlayer;
late Player testPlayer2;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate);
@@ -22,6 +23,7 @@ void main() {
withClock(fakeClock, () {
testPlayer = Player(name: 'Test Player');
testPlayer2 = Player(name: 'Second Group');
});
});
tearDown(() async {
@@ -30,39 +32,34 @@ void main() {
group('player tests', () {
test('all players get fetched correctly', () async {
await withClock(fakeClock, () async {
final testPlayer2 = Player(name: 'Second Group');
await database.playerDao.addPlayer(player: testPlayer);
await database.playerDao.addPlayer(player: testPlayer2);
await database.playerDao.addPlayer(player: testPlayer);
await database.playerDao.addPlayer(player: testPlayer2);
final allPlayers = await database.playerDao.getAllPlayers();
expect(allPlayers.length, 2);
final allPlayers = await database.playerDao.getAllPlayers();
expect(allPlayers.length, 2);
final fetchedPlayer1 = allPlayers.firstWhere(
(g) => g.id == testPlayer.id,
);
expect(fetchedPlayer1.name, testPlayer.name);
expect(fetchedPlayer1.createdAt, testPlayer.createdAt);
final fetchedPlayer1 = allPlayers.firstWhere(
(g) => g.id == testPlayer.id,
);
expect(fetchedPlayer1.name, testPlayer.name);
expect(fetchedPlayer1.createdAt, testPlayer.createdAt);
final fetchedPlayer2 = allPlayers.firstWhere(
(g) => g.id == testPlayer2.id,
);
expect(fetchedPlayer2.name, testPlayer2.name);
expect(fetchedPlayer2.createdAt, testPlayer2.createdAt);
});
final fetchedPlayer2 = allPlayers.firstWhere(
(g) => g.id == testPlayer2.id,
);
expect(fetchedPlayer2.name, testPlayer2.name);
expect(fetchedPlayer2.createdAt, testPlayer2.createdAt);
});
test('players get inserted correcly ', () async {
await withClock(fakeClock, () async {
await database.playerDao.addPlayer(player: testPlayer);
final result = await database.playerDao.getPlayerById(
playerId: testPlayer.id,
);
await database.playerDao.addPlayer(player: testPlayer);
final result = await database.playerDao.getPlayerById(
playerId: testPlayer.id,
);
expect(result.id, testPlayer.id);
expect(result.name, testPlayer.name);
expect(result.createdAt, testPlayer.createdAt);
});
expect(result.id, testPlayer.id);
expect(result.name, testPlayer.name);
expect(result.createdAt, testPlayer.createdAt);
});
test('players get deleted correcly ', () async {