Example #1
0
        private void describe_HandleAsync_UserRegistrationProcessCreated()
        {
            var userRegistrationProcessCreated = new UserRegistrationStarted(
                _userId,
                UserRegistrationFormMother.JohnDow());

            context["when user registration process is in Created state"] = () =>
            {
                before = () =>
                {
                    var process = UserRegistrationProcessMother.InCreatedState(_userId);
                    _userRegistrationProcessRepository.SaveAsync(process).Wait();
                };

                context["and user-by-email index accepts email"] = () =>
                {
                    before = () =>
                    {
                        _userByEmailIndex.SetResult(IndexResult.EmailAccepted);
                        _sut.HandleAsync(userRegistrationProcessCreated).Wait();
                    };

                    it["saves EmailAccepted event"] =
                        () => _eventStore.LastSavedEvents.Single().should_cast_to <EmailAccepted>();

                    it["is idempotent"] = () =>
                    {
                        _sut.HandleAsync(userRegistrationProcessCreated).Wait();
                        _eventStore.LastSavedEvents.should_be_empty();
                    };
                };

                context["and user-by-email index rejects email"] = () =>
                {
                    before = () =>
                    {
                        _userByEmailIndex.SetResult(IndexResult.EmailRejected);
                        _sut.HandleAsync(userRegistrationProcessCreated)
                        .Wait();
                    };

                    it["saves UserRegistrationFailed event"] = () =>
                                                               _eventStore.LastSavedEvents.Single().should_cast_to <UserRegistrationFailed>();

                    it["is idempotent"] = () =>
                    {
                        _sut.HandleAsync(userRegistrationProcessCreated).Wait();
                        _eventStore.LastSavedEvents.should_be_empty();
                    };
                };
            };
        }
        public async Task HandleAsync(IEvent @event)
        {
            if (@event == null)
            {
                throw new ArgumentNullException(nameof(@event));
            }

            if (@event is UserRegistrationStarted)
            {
                await _userRegistrationEventHandler.HandleAsync(@event as UserRegistrationStarted);
            }
            else if (@event is EmailAccepted)
            {
                await _userRegistrationEventHandler.HandleAsync(@event as EmailAccepted);
            }
            else if (@event is UserCreated)
            {
                await _userRegistrationEventHandler.HandleAsync(@event as UserCreated);
            }
        }