public void TestCreateVehicle() { //Test to see if a car type has been succeffully added int initialSize = VehicleService.GetAllVehicles().Count; VehicleService.CreateCar("car", "2020", "Nissan-GTR", 2, 4, "Body type", "V8"); int newSize = VehicleService.GetAllVehicles().Count; Assert.Equal(initialSize + 1, newSize); //Test to see if a non car type(e,g, boat) will throw a exception bool exceptionThrown = false; try { VehicleService.CreateCar("boat", "2005", "Toyota-GT86", 2, 4, "Body type", "V6"); } catch { exceptionThrown = true; } Assert.True(exceptionThrown); }
public void TestCreateVehicle() { //test if CreateVehicle is creating and adding the vehicle int initialSize = VehicleService.GetAllVehicles().Count; VehicleService.CreateCar("car", "test", "test", 2, 2, "test", "none"); int newSize = VehicleService.GetAllVehicles().Count; Assert.Equal(initialSize + 1, newSize); //test if CreateVehicle throws exception when the vehicle type is wrong bool exceptionThrown = false; try { VehicleService.CreateCar("not_car", "test", "test", 2, 2, "test", "none"); } catch { exceptionThrown = true; } Assert.True(exceptionThrown); }
public IActionResult CreateCar([FromBody] JsonElement vehicle) { string make = vehicle.GetProperty("Make").ToString(); string model = vehicle.GetProperty("Model").ToString(); int numOfDoors = int.Parse(vehicle.GetProperty("NumOfDoors").ToString()); int numOfWheels = int.Parse(vehicle.GetProperty("NumOfWheels").ToString()); string engine = vehicle.GetProperty("Engine").ToString(); string type = vehicle.GetProperty("Type").ToString(); string bodyType = vehicle.GetProperty("BodyType").ToString(); try { VehicleService.CreateCar(type, make, model, numOfDoors, numOfWheels, bodyType, engine); } catch { return(BadRequest()); } return(Ok()); }