From 4dbf75f0947bb4a54bbcd254ec42d0fe168ec3b5 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Thu, 14 Nov 2024 14:48:07 -0600 Subject: [PATCH] Fix tests to use mock channel and run structure --- .../test/games/bluetooth_collector_test.dart | 26 +++++++++++++------ .../test/games/cardinal_memory_test.dart | 7 +++++ .../test/games/code_collector_test.dart | 19 +++++++++++--- .../test/games/food_court_frenzy_test.dart | 20 +++++++++++--- .../test/games/orientation_memory_test.dart | 6 +++++ .../games/single_string_input_game_test.dart | 19 +++++++++----- .../test/games/string_collector_test.dart | 14 +++++++--- waydowntown_app/test/run_header_test.dart | 12 +++++++++ .../test/run_launch_route_test.dart | 21 ++------------- waydowntown_app/test/test_helpers.dart | 24 +++++++++++++++++ ...est.mocks.dart => test_helpers.mocks.dart} | 2 +- 11 files changed, 126 insertions(+), 44 deletions(-) rename waydowntown_app/test/{run_launch_route_test.mocks.dart => test_helpers.mocks.dart} (99%) diff --git a/waydowntown_app/test/games/bluetooth_collector_test.dart b/waydowntown_app/test/games/bluetooth_collector_test.dart index 77d7b3a4..a5909ef1 100644 --- a/waydowntown_app/test/games/bluetooth_collector_test.dart +++ b/waydowntown_app/test/games/bluetooth_collector_test.dart @@ -15,14 +15,11 @@ import 'package:waydowntown/games/bluetooth_collector.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; +@GenerateMocks([FlutterBluePlusMockable, BluetoothDevice, ScanResult]) import 'bluetooth_collector_test.mocks.dart'; -@GenerateMocks([ - FlutterBluePlusMockable, - BluetoothDevice, - ScanResult, -]) void main() { const submitAnswerRoute = '/waydowntown/submissions'; @@ -42,6 +39,8 @@ void main() { late FlutterBluePlusMockable mockFlutterBluePlus; + late MockPhoenixChannel mockChannel; + setUpAll(() { when(deviceResult1.device).thenReturn(device1); when(device1.platformName).thenReturn("Device 1"); @@ -64,6 +63,8 @@ void main() { dio.interceptors.add(PrettyDioLogger()); dioAdapter = DioAdapter(dio: dio); + (_, mockChannel, _) = TestHelpers.setupMockSocket(); + when(mockFlutterBluePlus.adapterState) .thenAnswer((_) => Stream.fromIterable([BluetoothAdapterState.on])); @@ -82,7 +83,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: BluetoothCollectorGame( - dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus), + dio: dio, + run: game, + flutterBluePlus: mockFlutterBluePlus, + channel: mockChannel), )); await tester.pumpAndSettle(); @@ -140,7 +144,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: BluetoothCollectorGame( - dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus), + dio: dio, + run: game, + flutterBluePlus: mockFlutterBluePlus, + channel: mockChannel), )); await tester.pumpAndSettle(); @@ -234,7 +241,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: BluetoothCollectorGame( - dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus), + dio: dio, + run: game, + flutterBluePlus: mockFlutterBluePlus, + channel: mockChannel), )); await tester.pumpAndSettle(); diff --git a/waydowntown_app/test/games/cardinal_memory_test.dart b/waydowntown_app/test/games/cardinal_memory_test.dart index 99eb355e..8f7a6a72 100644 --- a/waydowntown_app/test/games/cardinal_memory_test.dart +++ b/waydowntown_app/test/games/cardinal_memory_test.dart @@ -15,6 +15,7 @@ import 'package:waydowntown/models/answer.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; import 'cardinal_memory_test.mocks.dart'; @@ -29,6 +30,8 @@ void main() { late Run game; late MockMotionSensors mockMotionSensors; + late MockPhoenixChannel mockChannel; + setUp(() { mockMotionSensors = MockMotionSensors(); dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!)); @@ -39,6 +42,8 @@ void main() { const Answer(id: '2', label: 'west', order: 2), const Answer(id: '3', label: 'south', order: 3), ]); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('CardinalMemoryGame displays and submits pattern', @@ -97,6 +102,7 @@ void main() { home: CardinalMemoryGame( dio: dio, run: game, + channel: mockChannel, motionSensors: mockMotionSensors, ), )); @@ -193,6 +199,7 @@ void main() { home: CardinalMemoryGame( dio: dio, run: shortRun, + channel: mockChannel, motionSensors: mockMotionSensors, ), )); diff --git a/waydowntown_app/test/games/code_collector_test.dart b/waydowntown_app/test/games/code_collector_test.dart index a482e412..f4c42df8 100644 --- a/waydowntown_app/test/games/code_collector_test.dart +++ b/waydowntown_app/test/games/code_collector_test.dart @@ -14,6 +14,7 @@ import 'package:waydowntown/games/code_collector.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; import 'code_collector_test.mocks.dart'; @@ -24,6 +25,7 @@ void main() { late Dio dio; late DioAdapter dioAdapter; late Run game; + late MockPhoenixChannel mockChannel; late MockMobileScannerController mockController; @@ -36,6 +38,8 @@ void main() { dioAdapter = DioAdapter(dio: dio); game = TestHelpers.createMockRun( concept: 'code_collector', correctAnswers: 0, totalAnswers: 5); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('CodeCollectorGame displays scanned codes', @@ -56,7 +60,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: CodeCollectorGame( - dio: dio, run: game, scannerController: mockController), + dio: dio, + run: game, + channel: mockChannel, + scannerController: mockController), )); expect(find.text('Parent Region > Test Region'), findsOneWidget); @@ -133,7 +140,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: CodeCollectorGame( - dio: dio, run: game, scannerController: mockController), + dio: dio, + run: game, + channel: mockChannel, + scannerController: mockController), )); streamController.add(const BarcodeCapture( @@ -240,7 +250,10 @@ void main() { await tester.pumpWidget(MaterialApp( home: CodeCollectorGame( - dio: dio, run: game, scannerController: mockController), + dio: dio, + run: game, + channel: mockChannel, + scannerController: mockController), )); streamController.add(const BarcodeCapture( diff --git a/waydowntown_app/test/games/food_court_frenzy_test.dart b/waydowntown_app/test/games/food_court_frenzy_test.dart index 04145089..c992cd72 100644 --- a/waydowntown_app/test/games/food_court_frenzy_test.dart +++ b/waydowntown_app/test/games/food_court_frenzy_test.dart @@ -11,6 +11,7 @@ import 'package:waydowntown/models/answer.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; void main() { dotenv.testLoad(fileInput: File('.env').readAsStringSync()); @@ -20,6 +21,7 @@ void main() { late Dio dio; late DioAdapter dioAdapter; late Run run; + late MockPhoenixChannel mockChannel; setUp(() { dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!)); @@ -34,6 +36,8 @@ void main() { const Answer(id: '3', label: 'Sushi'), ], ); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('FoodCourtFrenzyGame displays and submits answers', @@ -74,7 +78,9 @@ void main() { ); await tester.pumpWidget( - MaterialApp(home: FoodCourtFrenzyGame(run: run, dio: dio)), + MaterialApp( + home: FoodCourtFrenzyGame(run: run, dio: dio, channel: mockChannel), + ), ); await tester.pumpAndSettle(); @@ -133,7 +139,9 @@ void main() { ); await tester.pumpWidget( - MaterialApp(home: FoodCourtFrenzyGame(run: run, dio: dio)), + MaterialApp( + home: FoodCourtFrenzyGame(run: run, dio: dio, channel: mockChannel), + ), ); await tester.pumpAndSettle(); @@ -176,7 +184,13 @@ void main() { ); await tester.pumpWidget( - MaterialApp(home: FoodCourtFrenzyGame(run: singleAnswerGame, dio: dio)), + MaterialApp( + home: FoodCourtFrenzyGame( + run: singleAnswerGame, + dio: dio, + channel: mockChannel, + ), + ), ); await tester.pumpAndSettle(); diff --git a/waydowntown_app/test/games/orientation_memory_test.dart b/waydowntown_app/test/games/orientation_memory_test.dart index a89a200a..57c1921e 100644 --- a/waydowntown_app/test/games/orientation_memory_test.dart +++ b/waydowntown_app/test/games/orientation_memory_test.dart @@ -15,6 +15,7 @@ import 'package:waydowntown/models/answer.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; import 'orientation_memory_test.mocks.dart'; @@ -28,6 +29,7 @@ void main() { late DioAdapter dioAdapter; late Run game; late MockMotionSensors mockMotionSensors; + late MockPhoenixChannel mockChannel; setUp(() { mockMotionSensors = MockMotionSensors(); @@ -39,6 +41,8 @@ void main() { const Answer(id: '2', label: 'right', order: 2), const Answer(id: '3', label: 'left', order: 3), ]); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('OrientationMemoryGame displays and submits pattern', @@ -97,6 +101,7 @@ void main() { home: OrientationMemoryGame( dio: dio, run: game, + channel: mockChannel, motionSensors: mockMotionSensors, ), )); @@ -195,6 +200,7 @@ void main() { home: OrientationMemoryGame( dio: dio, run: shortRun, + channel: mockChannel, motionSensors: mockMotionSensors, ), )); diff --git a/waydowntown_app/test/games/single_string_input_game_test.dart b/waydowntown_app/test/games/single_string_input_game_test.dart index 8590964b..f1f48c08 100644 --- a/waydowntown_app/test/games/single_string_input_game_test.dart +++ b/waydowntown_app/test/games/single_string_input_game_test.dart @@ -11,6 +11,7 @@ import 'package:waydowntown/models/answer.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; void main() { dotenv.testLoad(fileInput: File('.env').readAsStringSync()); @@ -20,6 +21,7 @@ void main() { late Dio dio; late DioAdapter dioAdapter; late Run run; + late MockPhoenixChannel mockChannel; setUp(() { dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!)); @@ -28,6 +30,8 @@ void main() { run = TestHelpers.createMockRun(concept: 'fill_in_the_blank', answers: [ const Answer(id: '1', label: 'An enormous headline proclaims ____ quit!') ]); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('Run is requested, displayed, and answers are posted', @@ -50,8 +54,9 @@ void main() { totalAnswers: 1, isComplete: true)); - await tester.pumpWidget( - MaterialApp(home: SingleStringInputGame(run: run, dio: dio))); + await tester.pumpWidget(MaterialApp( + home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel), + )); await tester.pumpAndSettle(); expect(tester.testTextInput.isRegistered, isTrue); @@ -102,8 +107,9 @@ void main() { isComplete: true, answerId: '1')); - await tester.pumpWidget( - MaterialApp(home: SingleStringInputGame(run: run, dio: dio))); + await tester.pumpWidget(MaterialApp( + home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel), + )); await tester.pumpAndSettle(); final textField = find.byType(TextField); @@ -134,8 +140,9 @@ void main() { concept: 'count_the_items', description: 'How many trees can you see in the courtyard?'); - await tester.pumpWidget( - MaterialApp(home: SingleStringInputGame(run: run, dio: dio))); + await tester.pumpWidget(MaterialApp( + home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel), + )); await tester.pumpAndSettle(); expect(find.text('Count the Items'), findsOneWidget); diff --git a/waydowntown_app/test/games/string_collector_test.dart b/waydowntown_app/test/games/string_collector_test.dart index 88d71768..dfd9c8f8 100644 --- a/waydowntown_app/test/games/string_collector_test.dart +++ b/waydowntown_app/test/games/string_collector_test.dart @@ -10,6 +10,7 @@ import 'package:waydowntown/games/string_collector.dart'; import 'package:waydowntown/models/run.dart'; import '../test_helpers.dart'; +import '../test_helpers.mocks.dart'; void main() { dotenv.testLoad(fileInput: File('.env').readAsStringSync()); @@ -19,6 +20,7 @@ void main() { late Dio dio; late DioAdapter dioAdapter; late Run game; + late MockPhoenixChannel mockChannel; setUp(() { dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!)); @@ -26,6 +28,8 @@ void main() { dioAdapter = DioAdapter(dio: dio); game = TestHelpers.createMockRun( concept: 'string_collector', description: 'Collect strings'); + + (_, mockChannel, _) = TestHelpers.setupMockSocket(); }); testWidgets('StringCollectorGame displays and submits strings', @@ -64,8 +68,9 @@ void main() { totalAnswers: 3, isComplete: true)); - await tester.pumpWidget( - MaterialApp(home: StringCollectorGame(run: game, dio: dio))); + await tester.pumpWidget(MaterialApp( + home: StringCollectorGame(run: game, dio: dio, channel: mockChannel), + )); await tester.pumpAndSettle(); expect(find.text('Parent Region > Test Region'), findsOneWidget); @@ -132,8 +137,9 @@ void main() { "error_string", game.id, null), ); - await tester.pumpWidget( - MaterialApp(home: StringCollectorGame(run: game, dio: dio))); + await tester.pumpWidget(MaterialApp( + home: StringCollectorGame(run: game, dio: dio, channel: mockChannel), + )); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextField), 'error_string'); diff --git a/waydowntown_app/test/run_header_test.dart b/waydowntown_app/test/run_header_test.dart index f223d109..598b47d1 100644 --- a/waydowntown_app/test/run_header_test.dart +++ b/waydowntown_app/test/run_header_test.dart @@ -23,6 +23,8 @@ void main() { correctSubmissions: 0, totalAnswers: 0, taskDescription: 'Test description', + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); @@ -46,6 +48,8 @@ void main() { correctSubmissions: 0, totalAnswers: 0, startedAt: DateTime.now().subtract(const Duration(seconds: 5)), + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); @@ -70,6 +74,8 @@ void main() { correctSubmissions: 0, totalAnswers: 0, startedAt: DateTime.now(), + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); @@ -93,6 +99,8 @@ void main() { correctSubmissions: 0, totalAnswers: 0, startedAt: DateTime.now().subtract(const Duration(seconds: 2)), + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); @@ -116,6 +124,8 @@ void main() { correctSubmissions: 1, totalAnswers: 2, taskDescription: 'Test description', + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); @@ -138,6 +148,8 @@ void main() { )), correctSubmissions: 0, totalAnswers: 1, + participations: [], + submissions: [], ); await tester.pumpWidget(MaterialApp(home: RunHeader(run: game))); diff --git a/waydowntown_app/test/run_launch_route_test.dart b/waydowntown_app/test/run_launch_route_test.dart index 6ede44e2..65126447 100644 --- a/waydowntown_app/test/run_launch_route_test.dart +++ b/waydowntown_app/test/run_launch_route_test.dart @@ -9,7 +9,6 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http_mock_adapter/http_mock_adapter.dart'; -import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; import 'package:phoenix_socket/phoenix_socket.dart'; @@ -20,12 +19,7 @@ import 'package:waydowntown/routes/run_launch_route.dart'; import 'package:waydowntown/services/user_service.dart'; import './test_helpers.dart'; -@GenerateNiceMocks([ - MockSpec(), - MockSpec(), - MockSpec(), -]) -import 'run_launch_route_test.mocks.dart'; +import './test_helpers.mocks.dart'; class TestAssetBundle extends CachingAssetBundle { final Map _assets = {}; @@ -76,7 +70,6 @@ void main() { late DioAdapter dioAdapter; late MockPhoenixSocket mockSocket; late MockPhoenixChannel mockChannel; - late MockPush mockPush; setUp(() async { dotenv.testLoad(fileInput: File('.env').readAsStringSync()); @@ -90,17 +83,7 @@ void main() { UserService.setUserData('user1', 'user1@example.com', false); UserService.setTokens('test_token', 'test_renewal_token'); - // Setup mock socket - mockSocket = MockPhoenixSocket(); - mockChannel = MockPhoenixChannel(); - mockPush = MockPush(); - - // Setup basic mock behaviors - when(mockSocket.connect()).thenAnswer((_) async => mockSocket); - when(mockSocket.addChannel(topic: anyNamed('topic'))) - .thenReturn(mockChannel); - when(mockChannel.join()).thenReturn(mockPush); - when(mockChannel.messages).thenAnswer((_) => const Stream.empty()); + (mockSocket, mockChannel, _) = TestHelpers.setupMockSocket(); const testMockStorage = './test/fixtures/core'; const channel = MethodChannel( diff --git a/waydowntown_app/test/test_helpers.dart b/waydowntown_app/test/test_helpers.dart index 605bfb7c..1457047c 100644 --- a/waydowntown_app/test/test_helpers.dart +++ b/waydowntown_app/test/test_helpers.dart @@ -1,4 +1,7 @@ import 'package:http_mock_adapter/http_mock_adapter.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'package:phoenix_socket/phoenix_socket.dart'; import 'package:waydowntown/models/answer.dart'; import 'package:waydowntown/models/participation.dart'; @@ -6,6 +9,13 @@ import 'package:waydowntown/models/region.dart'; import 'package:waydowntown/models/run.dart'; import 'package:waydowntown/models/specification.dart'; +@GenerateNiceMocks([ + MockSpec(), + MockSpec(), + MockSpec(), +]) +import 'test_helpers.mocks.dart'; + class TestHelpers { static void setupMockRunResponse( DioAdapter dioAdapter, { @@ -430,6 +440,20 @@ class TestHelpers { ], }; } + + static (MockPhoenixSocket, MockPhoenixChannel, MockPush) setupMockSocket() { + final mockSocket = MockPhoenixSocket(); + final mockChannel = MockPhoenixChannel(); + final mockPush = MockPush(); + + when(mockSocket.connect()).thenAnswer((_) async => mockSocket); + when(mockSocket.addChannel(topic: anyNamed('topic'))) + .thenReturn(mockChannel); + when(mockChannel.join()).thenReturn(mockPush); + when(mockChannel.messages).thenAnswer((_) => const Stream.empty()); + + return (mockSocket, mockChannel, mockPush); + } } class SubmissionRequest { diff --git a/waydowntown_app/test/run_launch_route_test.mocks.dart b/waydowntown_app/test/test_helpers.mocks.dart similarity index 99% rename from waydowntown_app/test/run_launch_route_test.mocks.dart rename to waydowntown_app/test/test_helpers.mocks.dart index 335ddc20..9059dafc 100644 --- a/waydowntown_app/test/run_launch_route_test.mocks.dart +++ b/waydowntown_app/test/test_helpers.mocks.dart @@ -1,5 +1,5 @@ // Mocks generated by Mockito 5.4.4 from annotations -// in waydowntown/test/run_launch_route_test.dart. +// in waydowntown/test/test_helpers.dart. // Do not manually edit this file. // ignore_for_file: no_leading_underscores_for_library_prefixes