public void WhenStoreIsPopulated_ReturnsSingleItemById() { //arrange var options = new DbContextOptionsBuilder <ApplicationContext>() .UseInMemoryDatabase(databaseName: "ReturnSingleItem") .Options; //act using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var items = GetTestCarAdverts().ToList(); items.ForEach(carAdvert => service.Add(carAdvert)); context.SaveChanges(); } //assert using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var testGuid = new Guid("981a7ab2-7887-4afd-b123-7d440c181627"); var carAdvert = service.GetById(testGuid); Assert.IsType <CarAdvert>(carAdvert); Assert.Equal(testGuid, carAdvert.Id); } }
public void Remove_WhenStoreIsPopulated_RemovessSingleItemById() { //arrange var options = new DbContextOptionsBuilder <ApplicationContext>() .UseInMemoryDatabase(databaseName: "RemoveSingleItem") .Options; //act using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var items = GetTestCarAdverts().ToList(); items.ForEach(carAdvert => service.Add(carAdvert)); context.SaveChanges(); } //assert using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var testGuid = new Guid("981a7ab2-7887-4afd-b123-7d440c181627"); service.Remove(testGuid); var deletedCarAdvert = service.GetById(testGuid); var allCarAdverts = service.GetAllItems().ToList(); Assert.Null(deletedCarAdvert); Assert.Equal(4, allCarAdverts.Count); } }
public void Update_WhenFieldsAreModified_ReturnsUpdatedFields() { //arrange var options = new DbContextOptionsBuilder <ApplicationContext>() .UseInMemoryDatabase(databaseName: "UpdateSingleItem") .Options; var testGuid = new Guid("981a7ab2-7887-4afd-b123-7d440c181627"); var newTitle = "Used Merceded Benz G50 for Sale"; var newFuel = FuelType.Gasoline; var newPrice = 200000; var newStatus = false; var newMileage = 105123; var newFirstRegistrationDate = new DateTime(2018, 3, 2); using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var items = GetTestCarAdverts().ToList(); items.ForEach(carAdvert => service.Add(carAdvert)); context.SaveChanges(); } //act using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var carAdvertToUpdate = service.GetById(testGuid); carAdvertToUpdate.Title = newTitle; carAdvertToUpdate.Fuel = newFuel; carAdvertToUpdate.Price = newPrice; carAdvertToUpdate.New = newStatus; carAdvertToUpdate.Mileage = newMileage; carAdvertToUpdate.FirstRegistration = newFirstRegistrationDate; service.Update(carAdvertToUpdate); } //assert using (var context = new ApplicationContext(options)) { var service = new CarAdvertService(context); var updatedCarAdvert = service.GetById(testGuid); Assert.Equal(updatedCarAdvert.Title, newTitle); Assert.Equal(updatedCarAdvert.Fuel, newFuel); Assert.Equal(updatedCarAdvert.Price, newPrice); Assert.Equal(updatedCarAdvert.New, newStatus); Assert.Equal(updatedCarAdvert.Mileage, newMileage); Assert.Equal(updatedCarAdvert.FirstRegistration, newFirstRegistrationDate); } }
public async Task CreateNewCarAdvertShouldBeSuccess() { var expected = GetCarAdvertDto(); var id = await _advertService.Add(expected); var obj = await _advertService.GetById(id); Assert.NotNull(obj); AssertEquals(expected, obj); }