예제 #1
0
        public async Task CreateUser_Success()
        {
            //arrange
            var command = new CreateUser
            {
                FirstName   = "FirstName",
                LastName    = "LastName",
                InitiatorId = GlobalAdmin.Id
            };
            var handler = new CreateUserHandler(EntityRepository, EventTransmitter);

            //act
            await handler.HandleAsync(command);

            //assert
            var entities = GetRecordedEntities <User>(EntityRepository, nameof(EntityRepository.AddAsync));

            Assert.That(entities.Count, Is.EqualTo(1));
            Assert.That(entities[0].Name, Is.EqualTo(command.Name));
            Assert.That(entities[0].IsShopManager, Is.False);
            Assert.That(entities[0].Shop, Is.Null);

            var events = GetRecordedEvents <DomainEvent <User> >();

            Assert.That(events.Count, Is.EqualTo(1));
            Assert.That(events[0].Trigger, Is.EqualTo(Trigger.Added));
            Assert.That(events[0].Entity, Is.EqualTo(entities[0]));
            Assert.That(events[0].RaisedBy, Is.EqualTo(command.InitiatorId));
        }
        public async Task CreatingUserWithDuplicatedEmailThrowsException()
        {
            CreateUser createUserCommand = new CreateUser(Guid.NewGuid(), "Manuel", "Berenguer Valero", "*****@*****.**", "loquesea", "España", "639903816", "03160");

            // We mock to consider the email as duplicated
            _checkEmailAvailability.Setup(x => x.IsAvailable(It.IsAny <string>()))
            .Returns(Task.FromResult <bool>(false));

            await Assert.ThrowsAsync <EmailAlreadyInUseException>(
                () => _createUserHandler.HandleAsync(createUserCommand)
                );
        }
예제 #3
0
        public async Task ShouldReturnErrorWhenCommandIsInvalid(CreateUserCommand command)
        {
            var fakeRepository = new Mock <IUserRepository>();

            fakeRepository
            .Setup(repository => repository.Exists(It.IsAny <string>()))
            .ReturnsAsync(false);

            var handler = new CreateUserHandler(fakeRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
예제 #4
0
        public async Task ShouldReturnSuccessWhenCommandIsValid(CreateUserCommand command)
        {
            var fakeRepository = new Mock <IUserRepository>();

            fakeRepository
            .Setup(repository => repository.Exists(It.IsAny <string>()))
            .ReturnsAsync(false);

            var handler = new CreateUserHandler(fakeRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Data.Should().NotBeNull();
            result.Success.Should().BeTrue();
            result.Errors.Should().BeNullOrEmpty();
            handler.Valid.Should().BeTrue();
        }
예제 #5
0
        public void AddsUserToRepository_RaisesIntegrationEvent()
        {
            var repository = new Mock <IUserRepository>(MockBehavior.Strict);

            repository.Setup(r => r.AddAsync(It.Is <Domain.User>(p => ValidateEquality(p)))).Returns(Task.CompletedTask).Verifiable();
            repository.Setup(r => r.SaveChanges()).Returns(Task.CompletedTask).Verifiable();

            var busPublisher = new Mock <IBusPublisher>(MockBehavior.Strict);

            busPublisher.Setup(p => p.Publish <IUserCreated>(It.Is <IUserCreated>(e => ValidateEquality(e)))).Returns(Task.CompletedTask).Verifiable();

            var handler = new CreateUserHandler(repository.Object, busPublisher.Object);
            var result  = handler.HandleAsync(Cmd, new Mock <ICorrelationContext>().Object).GetAwaiter().GetResult();

            Assert.IsTrue(result.Successful);
            repository.Verify();
            busPublisher.Verify();
        }
예제 #6
0
        public void Should_throw_validation_exception_when_validation_fails()
        {
            var command = new CreateUser
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                UserName = "******"
            };

            var userRepositoryMock = new Mock <IUserRepository>();

            var validatorMock = new Mock <IValidator <CreateUser> >();

            validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult(new List <ValidationFailure> {
                new ValidationFailure("Id", "Id Error")
            }));

            var createUserHandler = new CreateUserHandler(userRepositoryMock.Object, validatorMock.Object);

            Assert.ThrowsAsync <Exception>(async() => await createUserHandler.HandleAsync(command));
        }
예제 #7
0
        public async Task Should_validate_command_and_save_new_user()
        {
            var command = new CreateUser
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                UserName = "******"
            };

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.CreateAsync(It.IsAny <User>())).Returns(Task.FromResult(false));

            var validatorMock = new Mock <IValidator <CreateUser> >();

            validatorMock.Setup(x => x.Validate(command)).Returns(new ValidationResult());

            var createUserHandler = new CreateUserHandler(userRepositoryMock.Object, validatorMock.Object);
            await createUserHandler.HandleAsync(command);

            validatorMock.Verify(x => x.Validate(command));
            userRepositoryMock.Verify(x => x.CreateAsync(It.IsAny <User>()));
        }