public void Create_GivenInvalidData_ShouldThrowException() { // arrange int ownerId = 1; CarCreateViewModel model = new CarCreateViewModel() { Brand = "TestBrand", CarModel = "TestModel", Year = 1234, HasAirConditioner = true, Image = new Mock <IFormFile>().Object }; IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap <CarCreateViewModel, Car>() .ForMember(car => car.Id, opt => opt.Ignore()) .ForMember(car => car.Image, opt => opt.Ignore()))); Mock <IDbRepository <Car> > mockRepository = new Mock <IDbRepository <Car> >(); mockRepository .Setup(x => x.CreateAsync(It.IsAny <Car>())) .ThrowsAsync(new InvalidOperationException()); var service = new CarsService(mockRepository.Object, mapper); // act Func <Task> action = () => service.Create(model, ownerId); // assert action.Should().ThrowExactly <CarCreateException>() .WithMessage("Failed has car creating."); }
public void Create_GivenValidModelAndOwnerId_ShouldReturnCompletedTask() { // arrange int ownerId = 1; CarCreateViewModel model = new CarCreateViewModel() { Brand = "TestBrand", CarModel = "TestModel", Year = 1234, HasAirConditioner = true, Image = new Mock <IFormFile>().Object }; IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap <CarCreateViewModel, Car>() .ForMember(car => car.Id, opt => opt.Ignore()) .ForMember(car => car.Image, opt => opt.Ignore()))); Mock <IDbRepository <Car> > mockRepository = new Mock <IDbRepository <Car> >(); mockRepository .Setup(x => x.CreateAsync(It.IsAny <Car>())) .Returns(Task.FromResult(new Car())); var service = new CarsService(mockRepository.Object, mapper); // act Func <Task> action = async() => await service.Create(model, ownerId); // assert action.Should().NotThrow(); }
public ActionResult <Car> Create([FromBody] Car newCar) { try{ return(Ok(_service.Create(newCar))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public ActionResult <Car> Create([FromBody] Car newCar) { try{ return(Ok(_cs.Create(newCar))); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public ActionResult <Car> Create([FromBody] Car car) { try { return(Ok(_service.Create(car))); } catch (SystemException err) { return(BadRequest(err.Message)); } }
public ActionResult <Car> Create([FromBody] Car newCar) { try { Car addCar = _cs.Create(newCar); return(Ok(addCar)); } catch (Exception err) { return(BadRequest(err.Message)); } }
public void Create_GivenNullModel_ShouldThrowNullObjectException() { // arange int ownerId = 1; var service = new CarsService(null, null); // act Func <Task> action = () => service.Create(null, ownerId); // assert action.Should().ThrowExactly <NullObjectException>(); }
public ActionResult <Car> Create([FromBody] Car newCar) { try { return(Ok(_service.Create(newCar))); // // FakeDB.Cars.Add(newCar); // return Ok(newCar); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public void Create_GivenModelWithNullImage_ShouldThrowException() { // arrange int ownerId = 1; CarCreateViewModel model = new CarCreateViewModel(); CarsService service = new CarsService(null, null); // act Func <Task> action = () => service.Create(model, ownerId); // assert action.Should().Throw <Exception>(); }
public void Create_GivenNegativeOwnerId_ShouldThrowNegativeIntException() { // arrange int ownerId = -1; CarCreateViewModel model = new CarCreateViewModel(); CarsService service = new CarsService(null, null); // act Func <Task> action = () => service.Create(model, ownerId); // asert action.Should().ThrowExactly <NegativeIntException>(); }
public ActionResult <Car> Create([FromBody] Car newCar) { try { Claim user = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier); if (user == null) { throw new Exception("You must be logged in to make a car, yo."); } newCar.User = user.Value; return(Ok(_service.Create(newCar))); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public async Task <IActionResult> Create(IFormCollection collection) { try { var car = new CarDTO { CarNumber = collection["CarNumber"], CarModel = collection["CarModel"], EngineCapacity = Convert.ToDouble(collection["EngineCapacity"]), BodyNumber = collection["BodyNumber"], YearOfProduction = Convert.ToInt32(collection["YearOfProduction"]) }; await _carService.Create(car); _logger.LogInformation("Creation was successful."); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError("Creation failed.", ex); return(View()); } }
public ActionResult <Cars> Post([FromBody] Cars car) { return(_carsService.Create(car)); }
public ActionResult <Cars> Create(Cars cars) => _carsService.Create(cars);