public async Task PostCreated() { var serviceMock = new Mock <ILancamentoService>(); serviceMock.Setup(m => m.Post(It.IsAny <LancamentoDtoCreate>())).ReturnsAsync( new LancamentoDtoCreateResult { Id = Guid.NewGuid(), ContaId = Guid.NewGuid(), Description = "Crédito", Value = 100, CreateAt = DateTime.Now } ); _controller = new LancamentosController(serviceMock.Object); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var lancamentoDtoCreate = new LancamentoDtoCreate { ContaId = Guid.NewGuid(), Description = "Crédito", Value = 100 }; var result = await _controller.Post(lancamentoDtoCreate); Assert.True(result is CreatedResult); }
public async Task TestIntegrationLancamento() { var contaDtoCreate = new ContaDtoCreate() { Name = Faker.Name.FullName(), Description = "Conta Corrente", Status = true }; //Post var response = await PostJsonAsync(contaDtoCreate, $"{hostApi}contas", client); var postResult = await response.Content.ReadAsStringAsync(); var registroContaPost = JsonConvert.DeserializeObject <ContaDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(contaDtoCreate.Name, registroContaPost.Name); Assert.Equal(contaDtoCreate.Description, registroContaPost.Description); Assert.Equal(contaDtoCreate.Status, registroContaPost.Status); Assert.True(registroContaPost.Id != default(Guid)); var LancamentoDtoCreate = new LancamentoDtoCreate() { ContaId = registroContaPost.Id, Description = "Lancamento de crédito", Value = Faker.RandomNumber.Next(1, 1000) }; //Post response = await PostJsonAsync(LancamentoDtoCreate, $"{hostApi}lancamentos", client); postResult = await response.Content.ReadAsStringAsync(); var registroLancamentoPost = JsonConvert.DeserializeObject <LancamentoDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(LancamentoDtoCreate.ContaId, registroLancamentoPost.ContaId); Assert.Equal(LancamentoDtoCreate.Description, registroLancamentoPost.Description); Assert.Equal(LancamentoDtoCreate.Value, registroLancamentoPost.Value); Assert.True(registroLancamentoPost.Id != default(Guid)); //GET Id response = await client.GetAsync($"{hostApi}lancamentos/{registroLancamentoPost.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <LancamentoDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(registroLancamentoPost.Id, registroSelecionado.Id); }
public async Task <LancamentoDtoCreateResult> Post(LancamentoDtoCreate lancamento) { var contaEntity = await _repositoryConta.SelectAsync(lancamento.ContaId); if (contaEntity == null) { throw new Exception("Conta não encontrada!"); } contaEntity.Balance += lancamento.Value; var model = _mapper.Map <LancamentoModel>(lancamento); var entity = _mapper.Map <LancamentoEntity>(model); var result = await _repository.InsertLancamento(entity, contaEntity); return(_mapper.Map <LancamentoDtoCreateResult>(result)); }
public LancamentoTestes() { Id = Guid.NewGuid(); ContaId = Guid.NewGuid(); Description = "Lançamento de crédito"; Value = Faker.RandomNumber.Next(1, 1000); for (int i = 0; i < 10; i++) { var dto = new LancamentoDto() { Id = Guid.NewGuid(), ContaId = Guid.NewGuid(), Description = "Lançamento de crédito", Value = Faker.RandomNumber.Next(1, 1000) }; listaLancamentoDto.Add(dto); } lancamentoDto = new LancamentoDto { Id = Id, ContaId = ContaId, Description = Description, Value = Value }; lancamentoDtoCreate = new LancamentoDtoCreate { ContaId = ContaId, Description = Description, Value = Value }; lancamentoDtoCreateResult = new LancamentoDtoCreateResult { Id = Id, ContaId = ContaId, Description = Description, Value = Value, CreateAt = DateTime.UtcNow }; }
public async Task <ActionResult> Post([FromBody] LancamentoDtoCreate lancamento) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); // 400 bad request } var result = await _service.Post(lancamento); if (result != null) { return(Created(new Uri(Url.Link("GetLancamentoWithId", new { id = result.Id })), result)); } return(BadRequest()); } catch (ArgumentException ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }