public async Task delete_removes_corresponding_todo_from_inmemory_repository() { var todo = new Persistence.ToDo { Id = 100 }; await _dbContext.Database.EnsureDeletedAsync(); await _dbContext.Database.EnsureCreatedAsync(); _repository = new Repository <Persistence.ToDo>(_dbContext); _sutWithInMemory = new ToDoController(_repository); _sutWithInMemory.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "Test User"), new Claim(ClaimTypes.NameIdentifier, "*****@*****.**") })) } }; await _sutWithInMemory.Post(todo); await _sutWithInMemory.Delete(100); }
public async Task get_by_id_returns_corresponding_value_from_inmemory_repository() { var todo = new Persistence.ToDo { Id = 100, Content = "ToDo 1" }; await _dbContext.Database.EnsureDeletedAsync(); await _dbContext.Database.EnsureCreatedAsync(); _repository = new Repository <Persistence.ToDo>(_dbContext); _sutWithInMemory = new ToDoController(_repository); _sutWithInMemory.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "Test User"), new Claim(ClaimTypes.NameIdentifier, "*****@*****.**") })) } }; await _sutWithInMemory.Post(todo); var result = await _sutWithInMemory.Get(100); result.Id.Should().Be(100); }
public async Task put_throws_expception_if_todo_not_found() { var todo = new Persistence.ToDo { Id = 1, Content = "Updated Content" }; _mockRepository.Setup(x => x.GetAsync(1)) .ReturnsAsync(default(Persistence.ToDo)); _mockRepository.Setup(x => x.SaveAsync(todo)) .ReturnsAsync(todo) .Verifiable(); try { await _sut.Put(1, todo); Assert.Fail("Item not found exception not thrown"); } catch (Exception) { _mockRepository.Verify(x => x.SaveAsync(It.IsAny <Persistence.ToDo>()), Times.Never); } }
public async Task post_saves_todo_to_inmemory_repository() { var todo = new Persistence.ToDo(); var result = await _sutWithInMemory.Post(todo); result.Should().Be(todo); }
public async Task delete_removes_corresponding_todo_from_repository() { var todo = new Persistence.ToDo(); _mockRepository.Setup(x => x.RemoveAsync(todo)); await _sut.Delete(1); _mockRepository.Verify(); }
public async Task <Persistence.ToDo> Post([FromBody] Persistence.ToDo todo) { // For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803 todo.CreatedBy = User.Identity.Name; todo.CreatedOn = DateTime.UtcNow; todo.ModifiedBy = User.Identity.Name; todo.ModifiedOn = DateTime.UtcNow; return(await _repository.SaveAsync(todo)); }
public async Task get_by_id_returns_corresponding_value() { var todo = new Persistence.ToDo(); _mockRepository.Setup(x => x.GetAsync(1)) .ReturnsAsync(todo); var result = await _sut.Get(1); result.Should().Be(todo); _mockRepository.Verify(); }
public async Task post_saves_todo_to_repository() { var todo = new Persistence.ToDo(); _mockRepository.Setup(x => x.SaveAsync(todo)) .ReturnsAsync(todo) .Verifiable(); var result = await _sut.Post(todo); result.Should().Be(todo); _mockRepository.Verify(); }
public async Task put_updates_todo_to_inmemory_repository() { var todo = new Persistence.ToDo { Id = 1, Content = "Updated Content" }; await _sutWithInMemory.Post(new Persistence.ToDo()); var result = await _sutWithInMemory.Put(1, todo); result.Content.Should().Be("Updated Content"); }
public async Task <Persistence.ToDo> Put(int id, [FromBody] Persistence.ToDo todo) { // For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803 var existingTodo = await _repository.GetAsync(id); if (existingTodo == null) { throw new Exception(); } existingTodo.Content = todo.Content; existingTodo.IsCompleted = todo.IsCompleted; existingTodo.ModifiedBy = User.Identity.Name; existingTodo.ModifiedOn = DateTime.UtcNow; return(await _repository.SaveAsync(existingTodo)); }
public async Task put_updates_todo_to_repository() { var todo = new Persistence.ToDo { Id = 1, Content = "Updated Content" }; _mockRepository.Setup(x => x.GetAsync(1)) .ReturnsAsync(todo); _mockRepository.Setup(x => x.SaveAsync(todo)) .ReturnsAsync(todo) .Verifiable(); var result = await _sut.Put(1, todo); result.Should().Be(todo); _mockRepository.Verify(); }