public async Task <IActionResult> Post([FromBody] CriarPretendenteCommand model, CancellationToken cancellationToken) { if (ModelState.IsValid) { return(RetornarRequestResult(await _bus.Send(model, cancellationToken))); } return(BadRequest(model)); }
public async Task deve_retornar_ok_quando_adicionar_pretendente() { //arrange var comando = new CriarPretendenteCommand("Nome do Pretendente", "*****@*****.**"); _bus.Setup(p => p.Send(comando, _cancellationToken)).ReturnsAsync(new RequestResult().Ok()); //act var resultado = await _controller.Post(comando, _cancellationToken); //assert Assert.IsType <OkObjectResult>(resultado); _bus.Verify(p => p.Send(comando, _cancellationToken), Times.Once); }
public async Task deve_retornar_badrequest_quando_comando_invalido() { //arrange var comando = new CriarPretendenteCommand("", "*****@*****.**"); _bus.Setup(p => p.Send(comando, _cancellationToken)).ReturnsAsync(new RequestResult().BadRequest(new string[] { "nome é obrigatório" })); _controller.ModelState.AddModelError("Error", "ModelStateError"); //act var resultado = await _controller.Post(comando, _cancellationToken); //assert Assert.IsType <BadRequestObjectResult>(resultado); _bus.Verify(p => p.Send(comando, _cancellationToken), Times.Never); }
public async Task deve_retornar_erro_quando_email_ja_existe() { //arrange var comando = new CriarPretendenteCommand("Nome do pretendente", "*****@*****.**"); var evento = new PretendenteCriadoEvent(comando.Nome, comando.Email); var pretendeteDb = new Pretendente(comando.Nome, comando.Email); _pretendenteRepositorio.Setup(p => p.ObterPorEmailAsync(comando.Email)).ReturnsAsync(pretendeteDb); //act var resultado = await _handler.Handle(comando, _cancellationToken); //assert Assert.False(resultado.Sucesso); Assert.Equal(resultado.StatusCode, HttpStatusCode.BadRequest); Assert.Equal(resultado.Errors.FirstOrDefault(), "E-mail informado já esta em uso."); _pretendenteRepositorio.Verify(p => p.AdicionarAsync(It.IsAny <Pretendente>()), Times.Never); _bus.Verify(p => p.Publish(evento, _cancellationToken), Times.Never); }
public async Task deve_retornar_sucesso_quando_criar_pretendente() { //arrange var comando = new CriarPretendenteCommand("Nome do pretendente", "*****@*****.**"); var evento = new PretendenteCriadoEvent(comando.Nome, comando.Email); var pretendente = new Pretendente(comando.Nome, comando.Email); _bus.Setup(p => p.Publish(It.IsAny <PretendenteCriadoEvent>(), _cancellationToken)) .Callback <PretendenteCriadoEvent, CancellationToken>((re, ca) => { evento = re; _cancellationToken = ca; }); _pretendenteRepositorio.Setup(p => p.AdicionarAsync(pretendente)).Returns(Task.CompletedTask); //act var resultado = await _handler.Handle(comando, _cancellationToken); //assert Assert.True(resultado.Sucesso); Assert.Equal(resultado.StatusCode, HttpStatusCode.OK); Assert.Equal(resultado.Mensagem, "Operação realizada com sucesso."); _pretendenteRepositorio.Verify(p => p.AdicionarAsync(It.IsAny <Pretendente>()), Times.Once); _bus.Verify(p => p.Publish(evento, _cancellationToken), Times.Once); }