diff --git a/core/queue/test/queue_test.dart b/core/queue/test/queue_test.dart index caef0a0e..e3e94baf 100644 --- a/core/queue/test/queue_test.dart +++ b/core/queue/test/queue_test.dart @@ -9,23 +9,24 @@ import 'package:angel3_queue/src/queue.dart'; import 'package:angel3_queue/src/job_queueing_event.dart'; import 'package:angel3_queue/src/job_queued_event.dart'; import 'package:angel3_queue/src/should_queue_after_commit.dart'; -//import 'package:angel3_queue/src/invalid_payload_exception.dart'; import 'queue_test.mocks.dart'; -@GenerateMocks([Container, EventBus, MQClient, TransactionManager]) +@GenerateMocks([Container, MQClient, TransactionManager]) void main() { late MockContainer container; - late MockEventBus eventBus; + late EventBus eventBus; late MockMQClient mq; late TestQueue queue; + late List firedEvents; setUpAll(() { - provideDummy(MockEventBus()); + provideDummy(EventBus()); }); setUp(() { container = MockContainer(); - eventBus = MockEventBus(); + firedEvents = []; + eventBus = EventBus(); mq = MockMQClient(); queue = TestQueue(container, eventBus, mq); @@ -37,8 +38,11 @@ void main() { when(container.has()).thenReturn(false); when(container.make()).thenReturn(eventBus); - // Setup for EventBus mock - when(eventBus.fire(any)).thenAnswer((_) => Future.value()); + // Capture fired events + eventBus.on().listen((event) { + firedEvents.add(event); + print("Debug: Event fired - ${event.runtimeType}"); + }); // Setup for MQClient mock when(mq.sendMessage( @@ -47,7 +51,6 @@ void main() { routingKey: anyNamed('routingKey'), )).thenAnswer((_) { print("Debug: Mock sendMessage called"); - // Notice we're not returning anything here }); }); @@ -94,24 +97,17 @@ void main() { ); print("Debug: After enqueueUsing"); - // Verify all method calls in order - verifyInOrder([ - eventBus.fire(argThat(isA())), - eventBus.fire(argThat(isA())), - mq.sendMessage( - message: anyNamed('message'), - exchangeName: anyNamed('exchangeName'), - routingKey: anyNamed('routingKey'), - ), - ]); + // Wait for all events to be processed + await Future.delayed(Duration(milliseconds: 100)); - // Print captured arguments for sendMessage + // Verify that sendMessage was called final sendMessageCall = verify(mq.sendMessage( message: captureAnyNamed('message'), exchangeName: captureAnyNamed('exchangeName'), routingKey: captureAnyNamed('routingKey'), )); + // Print captured arguments for sendMessage if (sendMessageCall.captured.isNotEmpty) { print("sendMessage was called with:"); print("message: ${sendMessageCall.captured[0]}"); @@ -121,13 +117,23 @@ void main() { print("sendMessage was not called"); } - // Additional verifications - verify(eventBus.fire(any)).called(2); - verify(mq.sendMessage( - message: anyNamed('message'), - exchangeName: anyNamed('exchangeName'), - routingKey: anyNamed('routingKey'), - )).called(1); + // Print fired events for debugging + print("Fired events:"); + for (var event in firedEvents) { + print("${event.runtimeType}: ${event.toString()}"); + } + + // Verify fired events + expect(firedEvents, isNotEmpty, reason: "No events were fired"); + expect(firedEvents.where((event) => event is JobQueueingEvent).length, + equals(1), + reason: "JobQueueingEvent was not fired exactly once"); + expect( + firedEvents.where((event) => event is JobQueuedEvent).length, equals(1), + reason: "JobQueuedEvent was not fired exactly once"); + + // Verify that no other methods were called on mq + verifyNoMoreInteractions(mq); }); } @@ -176,9 +182,6 @@ class TestQueue extends Queue { ) async { eventBus.fire(JobQueueingEvent(connectionName, queue, job, payload, delay)); final result = await callback(payload, queue, delay); - eventBus.fire( - JobQueuedEvent(connectionName, queue, result, job, payload, delay)); - print("Attempting to send message..."); // Debug print mq.sendMessage( message: Message( @@ -191,7 +194,8 @@ class TestQueue extends Queue { routingKey: queue ?? 'default', ); print("Message sent."); // Debug print - + eventBus.fire( + JobQueuedEvent(connectionName, queue, result, job, payload, delay)); return result; } }