public void GetByIdShouldReturnNullOnEmptyCollection() { var repository = new Mock <IDeletableEntityRepository <Vehicle> >(); repository.Setup(r => r.All()).Returns(new List <Vehicle>().AsQueryable()); var service = new VehiclesService(repository.Object); int randomId = 1; Assert.Null(service.GetById(randomId)); }
public void GetByIdShouldReturnNullOnNonExistingId() { var repository = new Mock <IDeletableEntityRepository <Vehicle> >(); repository.Setup(r => r.All()).Returns(new List <Vehicle> { new Vehicle() { Id = 2 }, }.AsQueryable()); var service = new VehiclesService(repository.Object); int nonExistingId = 1; Assert.Null(service.GetById(nonExistingId)); }
public async Task CreateShouldAddTheCorrectObject() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MyTestDb2") .Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Vehicle>(dbContext); var service = new VehiclesService(repository); var newVehicle = new Vehicle { Id = 1, Make = "Mercedes", Model = "S500" }; await service.Create(newVehicle); var vehicleFromDb = service.GetById(newVehicle.Id); Assert.Equal <Vehicle>(newVehicle, vehicleFromDb); }
public async Task EditShouldDoNothingWithNonExistingId() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MyTestDb7") .Options; var dbContext = new ApplicationDbContext(options); dbContext.Vehicles.Add(new Vehicle { Id = 1, Make = "Mercedes" }); await dbContext.SaveChangesAsync(); var repository = new EfDeletableEntityRepository <Vehicle>(dbContext); var service = new VehiclesService(repository); var inputModel = new VehicleEditViewModel { Id = 2, Make = "BMW", }; await service.Edit(inputModel); Assert.Equal("Mercedes", service.GetById(1).Make); }
public async Task EditShouldUpdateEntityWithExistingId() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MyTestDb8") .Options; var dbContext = new ApplicationDbContext(options); var newVehicle = new Vehicle { Id = 1, Make = "Mercedes", Model = "S500", VIN = "WDB2221KZ8N654321", NumberPlate = "CA1111PA", FirstRegistration = new DateTime(2019, 1, 1), Type = VehicleType.Truck, }; dbContext.Vehicles.Add(newVehicle); await dbContext.SaveChangesAsync(); var repository = new EfDeletableEntityRepository <Vehicle>(dbContext); var service = new VehiclesService(repository); var inputModel = new VehicleEditViewModel { Id = 1, Make = "BMW", Model = "760Li", VIN = "WBNX6X6D30300G980", NumberPlate = "CA2222PA", FirstRegistration = "31-12-2018", Type = "Car", }; await service.Edit(inputModel); Assert.Equal("BMW", service.GetById(1).Make); Assert.Equal("760Li", service.GetById(1).Model); Assert.Equal("WBNX6X6D30300G980", service.GetById(1).VIN); Assert.Equal("CA2222PA", service.GetById(1).NumberPlate); var expectedDate = new DateTime(2018, 12, 31); Assert.Equal <DateTime>(expectedDate, service.GetById(1).FirstRegistration); Assert.Equal <VehicleType>(VehicleType.Car, service.GetById(1).Type); }