public async Task DeleteTest() { using (IDataContext context = new MockDataContext()) { using (var controller = new DevicesController(context)) { var device1 = new Device() { Name = "Device 1", Description = "Desc 1", DisplayOrder = 1, DateCreated = DateTime.Now }; var device2 = new Device() { Name = "Device 2", Description = "Desc 2", DisplayOrder = 2, DateCreated = DateTime.Now }; context.Devices.Add(device1); context.Devices.Add(device2); await context.SaveChangesAsync(); await controller.Delete(device1.Id); Assert.AreEqual(1, context.Devices.Count(), "Item was not deleted"); var actual = context.Devices.FirstOrDefault(); Assert.AreEqual(device2.Name, actual.Name, "Appears the wrong device was deleted"); Assert.ThrowsAsync<KeyNotFoundException>(async () => await controller.Delete(500)); } } }
public async Task PutTest() { var device = new Device() { Name = "Device 1", Description = "Device Description", DisplayOrder = 1, DateCreated = DateTime.Now, }; var device2 = new Device() { Name = "Device 2", Description = "Device Description", DisplayOrder = 2, DateCreated = DateTime.Now }; using (IDataContext context = new MockDataContext()) { using (var controller = new DevicesController(context)) { context.Devices.Add(device); context.Devices.Add(device2); await context.SaveChangesAsync(); var newDevice = new Device() { Id = device.Id, Name = "Test Device 1", Description = "Test Description 1", DisplayOrder = 3, DateCreated = DateTime.Now.AddDays(-200) }; await controller.Put(newDevice); var actual = context.Devices.FirstOrDefault(i => i.Id == device.Id); Assert.AreEqual(newDevice.Name, actual.Name, "Name was not updated"); Assert.AreEqual(newDevice.Description, actual.Description, "Description was not updated"); Assert.AreEqual(newDevice.DisplayOrder, actual.DisplayOrder, "Display Order was not updated"); Assert.AreEqual(device.DateCreated, actual.DateCreated, "DateCreated was updated and should not have been"); newDevice = new Device() { Id = device2.Id, Name = "Test Device2", Description = "Test Description 2", DisplayOrder = 1, DateCreated = DateTime.Now.AddDays(-201) }; await controller.Put(newDevice); actual = context.Devices.FirstOrDefault(i => i.Id == device2.Id); Assert.AreEqual(newDevice.Name, actual.Name, "Name was not updated"); Assert.AreEqual(newDevice.Description, actual.Description, "Description was not updated."); Assert.AreEqual(newDevice.DisplayOrder, actual.DisplayOrder, "Display Order was not updated."); Assert.AreEqual(device2.DateCreated, actual.DateCreated, "DateCreated was updated and should not have been"); newDevice = new Device() { Id = 500, Name = "Failed Update", Description = "Failed Update" }; Assert.ThrowsAsync<KeyNotFoundException>(async () => await controller.Put(newDevice)); } } }
public async Task GetByIdTest() { using (IDataContext context = new MockDataContext()) { using (var controller = new DevicesController(context)) { var device1 = new Device() { Name = "Device 1", Description = "Desc 1", DisplayOrder = 1, DateCreated = DateTime.Now }; var device2 = new Device() { Name = "Device 2", Description = "Desc 2", DisplayOrder = 2, DateCreated = DateTime.Now }; context.Devices.Add(device1); context.Devices.Add(device2); await context.SaveChangesAsync(); var actual = controller.Get(device1.Id); Assert.IsNotNull(actual); Assert.AreEqual(device1.Name, actual.Name, "Name does not match"); Assert.AreEqual(device1.Description, actual.Description, "Description does not match"); Assert.AreEqual(device1.DisplayOrder, actual.DisplayOrder, "Display Order does not match"); Assert.AreEqual(device1.DateCreated, actual.DateCreated, "DateCreated does not match"); actual = controller.Get(device2.Id); Assert.IsNotNull(actual); Assert.AreEqual(device2.Name, actual.Name, "Name does not match"); Assert.AreEqual(device2.Description, actual.Description, "Description does not match"); Assert.AreEqual(device2.DisplayOrder, actual.DisplayOrder, "Display Order does not match"); Assert.AreEqual(device2.DateCreated, actual.DateCreated, "DateCreated does not match"); } } }