public async Task PostById_Should_Create_Entity() { // Arrange var testSaveDto = new TestSaveDto { TestProperty = "New" }; // Act var result = await _testBaseController.PostByIdTest(testSaveDto, nameof(_testBaseController.GetByIdTest)); // Assert var actionResult = result as CreatedAtActionResult; Assert.IsNotNull(actionResult); Assert.AreEqual(nameof(_testBaseController.GetByIdTest), actionResult.ActionName); Assert.IsNull(actionResult.Value); var id = (Guid)actionResult.RouteValues["id"]; Assert.AreEqual(id, actionResult.RouteValues["id"]); using var testDbContext = new TestDbContext(ContextOptions); Assert.AreEqual(1, testDbContext.TestEntities.Count()); var entity = testDbContext.TestEntities.First(); Assert.AreEqual(id, entity.Id); Assert.AreEqual("New", entity.TestProperty); }
public async Task PutById_Should_Update_Entity() { // Arrange var testEntity1 = new TestEntity { Id = Guid.NewGuid() }; var testEntity2 = new TestEntity { Id = Guid.NewGuid() }; SeedDatabase(new List <TestEntity> { testEntity1, testEntity2 }); var testSaveDto = new TestSaveDto { Id = testEntity2.Id, TestProperty = "Updated" }; // Act var result = await _testBaseController.PutByIdTest(testEntity2.Id, testSaveDto); // Assert var noContentResult = result as NoContentResult; Assert.IsNotNull(noContentResult); using var testDbContext = new TestDbContext(ContextOptions); Assert.AreEqual(2, testDbContext.TestEntities.Count()); var updatedEntity = testDbContext.TestEntities.Single(t => t.Id == testEntity2.Id); var unchangedEntity = testDbContext.TestEntities.Single(t => t.Id == testEntity1.Id); Assert.AreEqual(testSaveDto.TestProperty, updatedEntity.TestProperty); Assert.IsNull(unchangedEntity.TestProperty); }