public async Task CreateAsync_given_character_adds_it() { var entity = default(Character); var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())).Callback <Character>(t => entity = t); using (var repository = new EntityFrameworkCharacterRepository(context.Object)) { var character = new CharacterCreateDTO { ActorId = 1, Name = "Name", Species = "Species", Planet = "Planet", Image = "Image" }; await repository.CreateAsync(character); } Assert.Equal(1, entity.ActorId); Assert.Equal("Name", entity.Name); Assert.Equal("Species", entity.Species); Assert.Equal("Planet", entity.Planet); Assert.Equal("Image", entity.Image); }
public ActionResult <CharacterReadDTO> CreateCharacter(CharacterCreateDTO characterCreateDTO) { var characterModel = _mapper.Map <Character>(characterCreateDTO); _repository.CreateCharacter(characterModel); _repository.SaveChanges(); return(Created("http://localhost:5000/characters", characterModel)); }
public async Task <IActionResult> Post([FromBody] CharacterCreateDTO character) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var id = await _repository.CreateAsync(character); return(CreatedAtAction(nameof(Get), new { id }, null)); }
public async Task <string> CreateAsync(CharacterCreateDTO character) { var response = await _client.PostAsync("api/characters", character.ToHttpContent()); if (response.IsSuccessStatusCode) { return(response.Headers.GetValues("Location").First()); } return(null); }
public async Task Post_given_valid_character_calls_CreateAsync() { var repository = new Mock <ICharacterRepository>(); var controller = new CharactersController(repository.Object); var character = new CharacterCreateDTO(); await controller.Post(character); repository.Verify(r => r.CreateAsync(character)); }
public async Task <int> CreateAsync(CharacterCreateDTO character) { var response = await _client.PostAsync("api/characters", character.ToHttpContent()); if (response.IsSuccessStatusCode) { var location = response.Headers.GetValues("Location").First(); return(int.Parse(location.Split('/').Last())); } return(-1); }
public async Task Post_given_invalid_character_returns_BadRequest() { var repository = new Mock <ICharacterRepository>(); var controller = new CharactersController(repository.Object); controller.ModelState.AddModelError(string.Empty, "Error"); var character = new CharacterCreateDTO(); var result = await controller.Post(character); Assert.IsType <BadRequestObjectResult>(result); }
public async Task Post_given_valid_character_returns_CreatedAtAction() { var repository = new Mock <ICharacterRepository>(); repository.Setup(r => r.CreateAsync(It.IsAny <CharacterCreateDTO>())).ReturnsAsync(42); var controller = new CharactersController(repository.Object); var character = new CharacterCreateDTO(); var result = await controller.Post(character) as CreatedAtActionResult; Assert.Equal(nameof(CharactersController.Get), result.ActionName); Assert.Equal(42, result.RouteValues["id"]); }
public async Task Post_given_invalid_character_does_not_call_CreateAsync() { var repository = new Mock <ICharacterRepository>(); var controller = new CharactersController(repository.Object); controller.ModelState.AddModelError(string.Empty, "Error"); var character = new CharacterCreateDTO(); await controller.Post(character); repository.Verify(r => r.CreateAsync(It.IsAny <CharacterCreateDTO>()), Times.Never); }
public ActionResult <CharacterReadDTO> UpdateCharacter(int id, CharacterCreateDTO updatedCharacter) { var character = _repository.GetCharacterById(id); if (character == null) { return(NotFound()); } _mapper.Map(updatedCharacter, character); _repository.UpdateCharacter(character); _repository.SaveChanges(); return(Ok(character)); }
public async Task <int> CreateAsync(CharacterCreateDTO character) { var entity = new Character { ActorId = character.ActorId, Name = character.Name, Species = character.Species, Planet = character.Planet, Image = character.Image }; _context.Characters.Add(entity); await _context.SaveChangesAsync(); return(entity.Id); }
public async Task Create_given_character_calls_SaveChangesAsync() { var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())); using (var repository = new EntityFrameworkCharacterRepository(context.Object)) { var character = new CharacterCreateDTO { Name = "Name", Species = "Species", }; await repository.CreateAsync(character); } context.Verify(c => c.SaveChangesAsync(default(CancellationToken))); }
public async Task Create_given_character_returns_new_Id() { var entity = default(Character); var context = new Mock <IFuturamaContext>(); context.Setup(c => c.Characters.Add(It.IsAny <Character>())) .Callback <Character>(t => entity = t); context.Setup(c => c.SaveChangesAsync(default(CancellationToken))) .Returns(Task.FromResult(0)) .Callback(() => entity.Id = 42); using (var repository = new EntityFrameworkCharacterRepository(context.Object)) { var character = new CharacterCreateDTO { Name = "Name", Species = "Species", }; var id = await repository.CreateAsync(character); Assert.Equal(42, id); } }