public async Task UpdateAsync_WithNonExistentId_ReturnsFalse() { // Arrange var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); context.CarModels.AddRange( new CarModel { Model = "OtherModel1" }, new CarModel { Model = "OtherModel2" }); context.SaveChanges(); var modelsService = new ModelsService(new EfRepository <CarModel>(context)); var serviceModel = new CarModelServiceModel { Id = Guid.NewGuid().ToString(), Model = "TestName" }; // Act var result = await modelsService.UpdateAsync(serviceModel); // Assert Assert.False(result); }
public async Task UpdateAsync_WithInvalidModel_DoesNotChange() { // Arrange const string expectedResult = "InitialName"; var model = new CarModel { Model = expectedResult }; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); context.CarModels.AddRange( model, new CarModel { Model = "OtherModel1" }, new CarModel { Model = "OtherModel2" }); context.SaveChanges(); var modelsService = new ModelsService(new EfRepository <CarModel>(context)); var serviceModel = new CarModelServiceModel { Id = model.Id, Model = "a" }; // Act var result = await modelsService.UpdateAsync(serviceModel); // Assert Assert.False(result); var actualResult = (await context.CarModels.SingleAsync(m => m.Id == model.Id)).Model; Assert.Equal(expectedResult, actualResult); }
public async Task <ActionResult> Edit(ModelPCB modelView) { if (ModelState.IsValid) { try { modelView.CreatedBy = User.Identity.Name; modelView.DateCreated = DateTime.Now; await modelService.UpdateAsync(modelView); } catch (Exception ex) { ModelState.AddModelError("ModelName", ex.Message); } return(RedirectToAction("Index")); } return(View()); }
public async Task UpdateAsyncCorrectlyUpdatesEntity() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("UpdateModelDb") .Options; var context = new NeedForCarsDbContext(options); var makesService = new MakesService(context); var modelsService = new ModelsService(context, makesService); var make = new Make { Name = "Make", Description = "Desc" }; await context.Makes.AddAsync(make); await context.SaveChangesAsync(); var model = new Model { Name = "Model1", Description = "Desc", MakeId = make.Id }; await context.Models.AddAsync(model); await context.SaveChangesAsync(); model.Name = "newName"; await modelsService.UpdateAsync(model); await context.SaveChangesAsync(); var result = context.Models .FirstAsync().Result .Name; Assert.Equal("newName", result); }