コード例 #1
0
 public ActionResult Create(int id)
 {
     ViewBag.RouteName = _routeService.Details(id).Name;
     RouteReview review = new RouteReview();
     review.RouteID = id;
     review.Rating = 3;
     return View(review);
 }
コード例 #2
0
        public void Test_Create_Post_InvalidModelState()
        {
            const string routeName = "Test Route";

            // Arrange
            IAccountService accountService = MockRepository.GenerateStub<IAccountService>();
            IRouteService routeService = MockRepository.GenerateStub<IRouteService>();
            IRouteReviewService routeReviewService = MockRepository.GenerateStub<IRouteReviewService>();
            routeService.Stub(s => s.Details(Arg<int>.Is.Anything)).Return(new Route { Name = routeName });
            RouteReviewController controller = new RouteReviewController(accountService, routeService, routeReviewService);
            RouteReview review = new RouteReview();
            controller.ModelState.AddModelError("key", "model is invalid");

            // Act
            ViewResult result = (ViewResult)controller.Create(review);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(routeName, result.ViewBag.RouteName);
        }
コード例 #3
0
        public ActionResult Create(RouteReview routeReview)
        {            
            if (ModelState.IsValid)
            {
                if (_routeReviewService.IsRouteReviewedByUser(routeReview.RouteID, Account.AccountID))
                {
                    ModelState.AddModelError("", "You have already written a review for this route.");
                    return View(routeReview);
                }

                routeReview.AccountID = Account.AccountID;
                routeReview = _routeReviewService.Insert(routeReview);

                return RedirectToAction("Details", "Route", new { id = routeReview.RouteID });
            }
            else
            {
                ViewBag.RouteName = _routeService.Details(routeReview.RouteID).Name;
                return View(routeReview);
            }
        }
コード例 #4
0
        public void Test_Create_Post_SavesReview()
        {
            // Arrange
            RouteReview review = new RouteReview() { RouteID = 1, Rating = 3 };
            IAccountService accountService = MockRepository.GenerateStub<IAccountService>();
            IRouteService routeService = MockRepository.GenerateStub<IRouteService>();
            IRouteReviewService routeReviewService = MockRepository.GenerateStub<IRouteReviewService>();
            routeReviewService.Stub(s => s.IsRouteReviewedByUser(Arg<int>.Is.Anything, Arg<int>.Is.Anything)).Return(
                false);
            routeReviewService.Stub(s => s.Insert(Arg<RouteReview>.Is.Anything)).Return(review);
            routeService.Stub(s => s.Details(Arg<int>.Is.Anything)).Return(new Route { RouteID = 1 });
            RouteReviewController controller = new RouteReviewController(accountService, routeService, routeReviewService);
            controller.Account = new Account() {AccountID = 1};

            // Act
            RedirectToRouteResult result = (RedirectToRouteResult)controller.Create(review);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Route", result.RouteValues["controller"]);
            Assert.AreEqual("Details", result.RouteValues["action"]);
            Assert.AreEqual(1, result.RouteValues["id"]);
        }
コード例 #5
0
        public RouteReview Insert(RouteReview routeReview)
        {
            try
            {
                routeReview.DateCreated = DateTime.Now;
                _routeReviewRepository.Insert(routeReview);

                Route route = _routeRepository.Find(routeReview.RouteID);
                List<int> ratings =
                    _routeReviewRepository.All.Where(rv => rv.RouteID == routeReview.RouteID).Select(r => r.Rating).
                        ToList();
                ratings.Add(routeReview.Rating);
                route.AverageRating = ratings.Average();
                _routeRepository.Update(route);
                _unitOfWork.Save();

                return routeReview;
            }
            catch (Exception ex)
            {
                throw new ServiceException("Unable to insert route review.", ex);
            }
            
        }
コード例 #6
0
        public void Test_Create_Post_ReviewExistsAddsModelError()
        {
            // Arrange
            RouteReview review = new RouteReview() { RouteID = 1, Rating = 3 };
            IAccountService accountService = MockRepository.GenerateStub<IAccountService>();
            IRouteService routeService = MockRepository.GenerateStub<IRouteService>();
            IRouteReviewService routeReviewService = MockRepository.GenerateStub<IRouteReviewService>();
            routeReviewService.Stub(s => s.IsRouteReviewedByUser(Arg<int>.Is.Anything, Arg<int>.Is.Anything)).Return(
                true);
            RouteReviewController controller = new RouteReviewController(accountService, routeService, routeReviewService);
            controller.Account = new Account() { AccountID = 1 };

            // Act
            ViewResult result = (ViewResult)controller.Create(review);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.ViewData.ModelState.Keys.Count);
            Assert.AreEqual(1, result.ViewData.ModelState[""].Errors.Count);
            Assert.AreEqual("You have already written a review for this route.", result.ViewData.ModelState[""].Errors[0].ErrorMessage);
        }
コード例 #7
0
        public void Test_Insert_Succeeds()
        {
            // Arrange
            List<RouteReview> testData = new List<RouteReview>
                                             {
                                                 new RouteReview
                                                     {
                                                         AccountID = 1,
                                                         RouteID = 2,
                                                         Rating = 2
                                                     }
                                             };
            Route route = new Route {RouteID = 2};
            IUnitOfWork unitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
            IRepository<RouteReview> routeReviewRepository = MockRepository.GenerateStub<IRepository<RouteReview>>();
            IRepository<Route> routeRepository = MockRepository.GenerateStub<IRepository<Route>>();
            unitOfWork.Stub(s => s.GetRepository<RouteReview>()).Return(routeReviewRepository);
            unitOfWork.Stub(s => s.GetRepository<Route>()).Return(routeRepository);
            routeRepository.Stub(s => s.Find(Arg<int>.Is.Anything)).Return(route);
            routeReviewRepository.Stub(s => s.All).Return(testData.AsQueryable());
            RouteReview review = new RouteReview
                                     {
                                         Rating = 4,
                                         DateCreated = DateTime.MinValue,
                                         RouteID = 2
                                     };
            RouteReviewService service = new RouteReviewService(unitOfWork);

            // Act
            RouteReview result = service.Insert(review);

            // Assert
            Assert.IsTrue(result.DateCreated != DateTime.MinValue);
            routeReviewRepository.AssertWasCalled(s => s.Insert(Arg<RouteReview>.Is.Anything));
            routeRepository.AssertWasCalled(s => s.Update(Arg<Route>.Is.Anything));
            unitOfWork.AssertWasCalled(s => s.Save());
        }
コード例 #8
0
        public void Test_Insert_ThrowsServiceExceptionOnUnitOfWorkError()
        {
            // Arrange
            List<RouteReview> testData = new List<RouteReview>
                                             {
                                                 new RouteReview
                                                     {
                                                         AccountID = 1,
                                                         RouteID = 2,
                                                         Rating = 2
                                                     }
                                             };
            Route route = new Route { RouteID = 2 };
            IUnitOfWork unitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
            IRepository<RouteReview> routeReviewRepository = MockRepository.GenerateStub<IRepository<RouteReview>>();
            IRepository<Route> routeRepository = MockRepository.GenerateStub<IRepository<Route>>();
            unitOfWork.Stub(s => s.GetRepository<RouteReview>()).Return(routeReviewRepository);
            unitOfWork.Stub(s => s.GetRepository<Route>()).Return(routeRepository);
            unitOfWork.Stub(s => s.Save()).Throw(new Exception());
            routeRepository.Stub(s => s.Find(Arg<int>.Is.Anything)).Return(route);
            routeReviewRepository.Stub(s => s.All).Return(testData.AsQueryable());
            RouteReview review = new RouteReview
            {
                Rating = 4,
                DateCreated = DateTime.MinValue,
                RouteID = 2
            };
            RouteReviewService service = new RouteReviewService(unitOfWork);
            bool exceptionThrown = false;

            // Act
            try
            {
                service.Insert(review);
            }
            catch (ServiceException)
            {
                exceptionThrown = true;
            }
            

            // Assert
            Assert.IsTrue(exceptionThrown);
        }
コード例 #9
0
        public void Test_Insert_SetsAverageRating()
        {
            // Arrange
            List<RouteReview> testData = new List<RouteReview>
                                             {
                                                 new RouteReview
                                                     {
                                                         AccountID = 1,
                                                         RouteID = 2,
                                                         Rating = 2
                                                     }
                                             };
            Route route = new Route { RouteID = 2 };
            IUnitOfWork unitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
            IRepository<RouteReview> routeReviewRepository = MockRepository.GenerateStub<IRepository<RouteReview>>();
            IRepository<Route> routeRepository = MockRepository.GenerateStub<IRepository<Route>>();
            unitOfWork.Stub(s => s.GetRepository<RouteReview>()).Return(routeReviewRepository);
            unitOfWork.Stub(s => s.GetRepository<Route>()).Return(routeRepository);
            routeRepository.Stub(s => s.Find(Arg<int>.Is.Anything)).Return(route);
            routeReviewRepository.Stub(s => s.All).Return(testData.AsQueryable());
            RouteReview review = new RouteReview
            {
                Rating = 4,
                DateCreated = DateTime.MinValue,
                RouteID = 2
            };
            RouteReviewService service = new RouteReviewService(unitOfWork);

            // Act
            service.Insert(review);

            // Assert
            Assert.AreEqual(3, route.AverageRating);
        }