public void Test_IsRouteReviewedByUser_ReturnsFalse()
        {
            // Arrange
            List<RouteReview> testData = new List<RouteReview>
                                             {
                                                 new RouteReview
                                                     {
                                                         AccountID = 1,
                                                         RouteID = 2
                                                     }
                                             };
            IUnitOfWork unitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
            IRepository<RouteReview> routeReviewRepository = MockRepository.GenerateStub<IRepository<RouteReview>>();
            unitOfWork.Stub(s => s.GetRepository<RouteReview>()).Return(routeReviewRepository);
            routeReviewRepository.Stub(s => s.All).Return(testData.AsQueryable());
            RouteReviewService service = new RouteReviewService(unitOfWork);

            // Act
            bool result = service.IsRouteReviewedByUser(1, 2);

            // Assert
            Assert.IsFalse(result);
        }
        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());
        }
        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);
        }
        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);
        }