public void ReturnsRedirectToActionResult_WhenModelStateIsValidAndCarIsFound()
            {
                var car = new Car {
                    Id = 1
                };
                var model   = new EnquiryAddViewModel();
                var enquiry = new Enquiry {
                    Id = 1
                };

                _carRepositoryMock.Setup(r => r.Get(car.Id)).Returns(car);
                _mapperMock.Setup(m => m.Map <EnquiryAddViewModel, Enquiry>(model)).Returns(enquiry);
                _enquiryRepositoryMock.Setup(r => r.Save(enquiry));

                var result = (RedirectToActionResult)_controller.Add(id: car.Id, model: model);

                Assert.IsType <RedirectToActionResult>(result);
                Assert.Equal("ThankYou", result.ActionName);
            }
Ejemplo n.º 2
0
        public IActionResult Add(int?id, EnquiryAddViewModel model)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            if (ModelState.IsValid)
            {
                var car = _carRepository.Get(id.Value);
                if (car == null)
                {
                    return(this.NotFound());
                }

                var enquiry = _mapper.Map <EnquiryAddViewModel, Enquiry>(model);
                enquiry.Car = car;
                _enquiryRepository.Save(enquiry);
                return(this.RedirectToAction("ThankYou"));
            }

            return(this.RedirectToAction("Details", "Car", new { id }));
        }