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 Create_Country_Should_Add_Country_To_Database() { var command = new CreateCountryCommand { Name = "Poland" }; var commandHandler = new CreateCountryCommandHandler(_context, _mapper); await commandHandler.Handle(command, CancellationToken.None); var result = _context.Countries .Where(x => x.Name.Equals(command.Name)) .FirstOrDefault(); result.ShouldNotBeNull(); }
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()); }
public CreateCountryCommandHandlerTests() { mediator = new Mock <IMediator>(); sut = new CreateCountryCommandHandler(context, mediator.Object); }