Example #1
0
        public void SendCommand_WithStoredEvents_AppliesEventsBeforeHandlingCommand()
        {
            // Arrange
            var stubEventStore = new FakeEventStore();
            var mockEvent      = new FakeEvent();

            stubEventStore.Save(new AdditionalEvents(Guid.NewGuid(),
                                                     new ArrayList()
            {
                mockEvent
            }));
            var stubMessageDispatcher = new EventSourcedMessageDispatcher(stubEventStore);

            stubMessageDispatcher
            .RegisteredHandlerFor <AppliesFakeEventHandlesFakeCommand, FakeCommand>();
            var mockCommand = new FakeCommand();

            // Act
            stubMessageDispatcher.SendCommand(mockCommand);

            // Assert
            Assert.Equal(mockEvent,
                         AppliesFakeEventHandlesFakeCommand.applied_event);
            Assert.Equal(mockCommand,
                         AppliesFakeEventHandlesFakeCommand.handled_command);
        }
Example #2
0
        public void SendCommand_WithoutRegisteredHandler_ThrowsNoHandlerRegisteredException()
        {
            // Arrange
            var fakeMessageDispatcher = new EventSourcedMessageDispatcher(
                new FakeEventStore());
            var mockCommand = new FakeCommand();

            // Act
            Action sendUnhandledCommand = () => fakeMessageDispatcher
                                          .SendCommand(mockCommand);

            // Assert
            Assert.Throws <NoHandlerRegistered>(sendUnhandledCommand);
        }
Example #3
0
        public void SendCommand_WithRegisteredHandler_IsHandledByRegisteredHandler()
        {
            // Arrange
            var fakeMessageDispatcher = new EventSourcedMessageDispatcher(
                new FakeEventStore());
            var mockCommand = new FakeCommand();

            fakeMessageDispatcher
            .RegisteredHandlerFor <HandlesFakeCommand, FakeCommand>();

            // Act
            fakeMessageDispatcher.SendCommand(mockCommand);

            // Assert
            Assert.Equal(mockCommand, HandlesFakeCommand.handled_command);
        }
Example #4
0
        public void RegisterHandlerFor_SameCommand_ThrowsCommandHandlerAlreadyRegisteredException()
        {
            // Arrange
            var fakeMessageDispatcher = new EventSourcedMessageDispatcher(
                new FakeEventStore());

            fakeMessageDispatcher
            .RegisteredHandlerFor <FirstFakeCommandHandler, FakeCommand>();

            // Act
            Action RegisterHandlerForSameCommand = () => fakeMessageDispatcher
                                                   .RegisteredHandlerFor <SecondFakeCommandHandler, FakeCommand>();

            // Assert
            Assert.Throws <CommandHandlerAlreadyRegistered>(
                RegisterHandlerForSameCommand);
        }
Example #5
0
        public void ScanInstance_WithEventSubscriber_RegistersEventSubscriber()
        {
            // Arrange
            var stubMessageDispatcher = new EventSourcedMessageDispatcher(
                new FakeEventStore());
            var mockGuid = Guid.NewGuid();

            stubMessageDispatcher.ScanInstance(new HandlesFakeCommandEmitsFakeEvents());
            stubMessageDispatcher.ScanInstance(new StaticFakeEventSubscriber());

            // Act
            stubMessageDispatcher.SendCommand(
                new FakeCommand()
            {
                Id = mockGuid
            });

            // Assert
            Assert.Equal(mockGuid, StaticFakeEventSubscriber.handled_event.Id);
        }
Example #6
0
        public void SendCommand_WithCommandThatEmitsEvent_SavesEventInEventStore()
        {
            // Arrange
            var stubEventStore        = new FakeEventStore();
            var stubMessageDispatcher = new EventSourcedMessageDispatcher(stubEventStore);
            var mockGuid = Guid.NewGuid();

            stubMessageDispatcher
            .RegisteredHandlerFor <HandlesFakeCommandEmitsFakeEvents, FakeCommand>();

            // Act
            stubMessageDispatcher.SendCommand(
                new FakeCommand()
            {
                Id = mockGuid
            });

            // Assert
            Assert.Equal(mockGuid, ((FakeEvent)(stubEventStore
                                                .LoadEventsFor(mockGuid) as ArrayList)[0]).Id);
        }
Example #7
0
        public void RegisterSubscriberFor_EventEmittedFromCommand_HandlesCommand()
        {
            // Arrange
            var stubMessageDispatcher = new EventSourcedMessageDispatcher(
                new FakeEventStore());
            var mockSubscriber = new SubscribesToFakeEvent();
            var mockGuid       = Guid.NewGuid();

            stubMessageDispatcher
            .RegisterSubscriberFor <FakeEvent>(mockSubscriber);
            stubMessageDispatcher
            .RegisteredHandlerFor <HandlesFakeCommandEmitsFakeEvents, FakeCommand>();

            // Act
            stubMessageDispatcher.SendCommand(
                new FakeCommand()
            {
                Id = mockGuid
            });

            // Assert
            Assert.Equal(mockGuid, mockSubscriber.handled_event.Id);
        }