Пример #1
0
        public async Task AddUserCommandHandler_UserAlreadyExists_AndNotHasCHanges_ShouldReturn_UserId()
        {
            var user    = new UserBuild().Builder();
            var command = new AddUserCommandBuilder()
                          .WithExternalId(user.ExternalId)
                          .WithName(user.Name)
                          .WithPhoneNumber(user.PhoneNumber)
                          .WithEmail(user.Email)
                          .Builder();

            repositoryMock.GetByExternalIdAsync(Arg.Any <string>()).Returns(user);
            var result = await handler.Handle(command, new CancellationToken());

            Assert.True(result.Success);
            Assert.AreEqual(user.Id, (Guid)result.Data);
        }
Пример #2
0
        public async Task AddUserHandler_Should_Add_User()
        {
            // Arrange...
            Domain.Models.User addedUser = _fixture.Create <Domain.Models.User>();
            var mockUnitOfWork           = Substitute.For <IUnitOfWork>();

            mockUnitOfWork.UserRepository.When(x => x.Create(Arg.Any <User>()))
            .Do(x => x.Arg <User>().Id = addedUser.Id);
            mockUnitOfWork.CompleteAsync().Returns(1);
            var mockMapper = Substitute.For <IMapper>();

            mockMapper.Map <Domain.Models.User>(Arg.Any <Application.Commands.AddUserCommand>()).Returns(addedUser);

            var sut            = new AddUserCommandHandler(mockUnitOfWork, mockMapper);
            var addUserCommand = _fixture.Create <AddUserCommand>();
            var ct             = new CancellationToken();

            // Act...
            var actualUserId = await sut.Handle(addUserCommand, ct);

            // Assert...
            actualUserId.Should().Be(addedUser.Id);
            mockUnitOfWork.UserRepository.Received(1).Create(Arg.Any <User>());
            await mockUnitOfWork.Received(1).CompleteAsync();
        }
Пример #3
0
        public async Task Add_New_User_When_User_DoesNot_Exist()
        {
            var expectedUser =
                new EntityFactory().NewUser(new Name("user-name"), new Email("email"), new Password("pass"), new PasswordSalt("salt"));

            _userRepositoryMock
            .Setup(x => x.SaveAsync(It.IsAny <User>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedUser);
            _userRepositoryMock
            .Setup(x => x.SelectByEmailAsync(expectedUser.Email, It.IsAny <CancellationToken>()))
            .ReturnsAsync(User.None);

            var actualResult = await _sut.Handle(new AddUserCommand("user-name", "email", "pass"), CancellationToken.None);

            actualResult.Should()
            .BeOfType <AddUserCommandResult>()
            .Which.User
            .Should().BeEquivalentTo(expectedUser);
            _userRepositoryMock.Verify(x => x.SelectByEmailAsync(It.IsAny <Email>(), It.IsAny <CancellationToken>()), Times.Once);
            _userRepositoryMock.Verify(x => x.SaveAsync(It.IsAny <User>(), It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task AddUserHandler_Should_Add_User()
        {
            // Arrange...
            var unitOfWork     = GetUnitOfWork(out var inMemoryContext);
            var mapper         = GetMapper();
            var sut            = new AddUserCommandHandler(unitOfWork, mapper);
            var addUserCommand = _fixture.Create <AddUserCommand>();
            var ct             = new CancellationToken();

            // Act...
            var addedUserId = await sut.Handle(addUserCommand, ct);

            // Assert...
            inMemoryContext.Users.Count().Should().Be(1);
        }
        public async void ShouldAddUser()
        {
            var fixture = new Fixture();

            using (DatabaseContext dbContext = DbContextHelpers.CreateDatabaseContext())
            {
                var command = fixture.Create <AddUserCommand>();
                var handler = new AddUserCommandHandler(dbContext);

                await handler.Handle(command, CancellationToken.None);

                var created = await dbContext.Users.FindAsync(command.UserId);

                created.Id.Should().Be(command.UserId);
            }
        }
Пример #6
0
        public void AddUserHandler_Should_Throw_When_Command_Is_Null()
        {
            // Arrange...
            var mockUnitOfWork = Substitute.For <IUnitOfWork>();
            var mockMapper     = Substitute.For <IMapper>();

            var sut = new AddUserCommandHandler(mockUnitOfWork, mockMapper);

            const AddUserCommand addUserCommand = null;
            var ct = new CancellationToken();

            // Act...
            Func <Task> action = async() => await sut.Handle(addUserCommand, ct);

            // Assert...
            action.Should().Throw <ArgumentNullException>();
        }
        public async Task AddUserCommandHandlerShouldReturnUserObject()
        {
            var user = new User()
            {
                Password = "******",
                Username = "******"
            };

            var userCommand = new AddUserCommand(user)
            {
                User = user
            };

            var handler = new AddUserCommandHandler(_userRepositoryMock.Object);

            _userRepositoryMock.Setup(a => a.Add(userCommand.User)).ReturnsAsync(user);

            var result = await handler.Handle(userCommand, new CancellationToken());

            result.Should().BeOfType <User>();
        }
Пример #8
0
        public void CreateUser_Successfully(User user)
        {
            var handler = new AddUserCommandHandler(base.UnitOfWorkMock.Object);

            var result = handler.Handle(new AddUserCommand()
            {
                User = new BasicUserVm()
                {
                    Name            = user.Name,
                    Email           = user.Email,
                    Lastname        = user.Lastname,
                    TelephoneNumber = user.TelephoneNumber,
                    Age             = user.Age,
                    Address         = user.Address.Select(u => new AddressVm {
                        Description = u.Description
                    }).ToList()
                }
            }, default);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Task <Response <UserVm> > >(result);
            Assert.AreEqual(StatusCodes.Status200OK, result.Result.StatusCode);
        }