Ejemplo n.º 1
0
        public async Task Handle_ValidCommand_ShouldAddToRepository()
        {
            var mockCityRepository = RepositoryMocks.GetCityRepository();

            mockCityRepository.Setup(repo => repo.IsCityNameUnique(It.IsAny <string>())).ReturnsAsync(true);

            var mockCountryRepository = RepositoryMocks.GetCountryRepository();

            mockCountryRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(new Country {
                CountryId = Guid.Parse("{3ae4e108-e2df-4893-958a-2d76ab89b9dc}"), Name = "Italy"
            });

            var initialListCount = (await mockCityRepository.Object.GetAllAsync()).Count;

            var handler = new CreateCityCommandHandler(_mapper, mockCityRepository.Object, mockCountryRepository.Object);

            var result = await handler.Handle(new CreateCityCommand { Name = "Naples", CountryId = Guid.Parse("{3ae4e108-e2df-4893-958a-2d76ab89b9dc}") }, CancellationToken.None);

            result.Should().BeOfType(typeof(CityDto));
            result.Name.Should().Be("Naples");

            var cities = await mockCityRepository.Object.GetAllAsync();

            cities.Count.Should().Be(initialListCount + 1);

            mockCityRepository.Verify(repo => repo.AddAsync(It.IsAny <City>()), Times.Once());
        }
        public GetCountryDetailTests()
        {
            var configurationProvider = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <MappingProfile>();
            });

            _mapper = configurationProvider.CreateMapper();

            _mockCountryRepository = RepositoryMocks.GetCountryRepository();
        }
        public void Handle_InvalidCommand_ShouldThrowValidationException()
        {
            var mockCountryRepository = RepositoryMocks.GetCountryRepository();

            mockCountryRepository.Setup(repo => repo.IsCountryNameUnique(It.IsAny <string>())).ReturnsAsync(false);

            var handler = new CreateCountryCommandHandler(_mapper, mockCountryRepository.Object);

            Func <Task> func = async() => await handler.Handle(new CreateCountryCommand { Name = "" }, CancellationToken.None);

            func.Should().Throw <ValidationException>().Where(e => e.Errors.Count == 2 &&
                                                              e.Errors.Any(x => x.Contains("Name cannot be empty")) &&
                                                              e.Errors.Any(x => x.Contains("Country with that name already exists")));

            mockCountryRepository.Verify(repo => repo.AddAsync(It.IsAny <Country>()), Times.Never());
        }
        public async Task Handle_ValidCommand_ShouldAddToRepository()
        {
            var mockCountryRepository = RepositoryMocks.GetCountryRepository();

            mockCountryRepository.Setup(repo => repo.IsCountryNameUnique(It.IsAny <string>())).ReturnsAsync(true);

            var initialListCount = (await mockCountryRepository.Object.GetAllAsync()).Count;

            var handler = new CreateCountryCommandHandler(_mapper, mockCountryRepository.Object);

            var result = await handler.Handle(new CreateCountryCommand { Name = "Naples" }, CancellationToken.None);

            result.Should().BeOfType(typeof(CountryDto));
            result.Name.Should().Be("Naples");

            var countries = await mockCountryRepository.Object.GetAllAsync();

            countries.Count.Should().Be(initialListCount + 1);

            mockCountryRepository.Verify(repo => repo.AddAsync(It.IsAny <Country>()), Times.Once());
        }