Exemplo n.º 1
0
        public void EditTest1()
        {
            var controller   = new RoutesController();
            var result       = controller.Edit(1) as ViewResult;
            var preEditRoute = result.Model as Route;

            preEditRoute.RouteName = "I changed this";

            controller.Edit(preEditRoute);

            var postEditRoute = _db.Routes.Find(r => r.Id == 1);

            Assert.IsTrue(postEditRoute.RouteName == "I changed this");
        }
        public void Get_Edit_WithValidDataShouldReturnEditRouteFormView()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, this.townService.Object, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.routeService.Setup(r => r.GetRouteToEdit(It.IsAny <string>(), It.IsAny <int>()))
            .Returns(this.GetRouteToEdit());

            this.townService.SetupSequence(t => t.GetTownNameByStationId(It.IsAny <int>()))
            .Returns(StartTownName)
            .Returns(EndTownName);

            this.routeService.Setup(r => r.HasReservedTickets(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(false);

            //Act
            var result = controller.Edit(RouteId);

            //Assert
            result.Should().BeOfType <ViewResult>();
            var viewResult = result.As <ViewResult>().Model;
            var form       = viewResult.As <RouteFormModel>();

            this.AssertRouteFormModelProperties(StartStation, EndStation, form, viewResult);
            form.IsEdit.Should().BeTrue();
        }
        public void Post_Edit_WhenCompanyIsBlockedShouldRedirectToAllRoutes()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, null, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(true);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(null, RouteId);

            //Assert
            this.AssertRedirectToCompanyRoutes(result);
            this.customMessage.Should().Be(WebConstants.Message.ChangeRouteCompanyBlocked);
        }
        public void Post_Edit_WhenThereIsAlreadySameRouteShouldReturnEditRouteFormView()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, this.townService.Object, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.townService.Setup(t => t.GetTownsWithStations())
            .Returns(this.GetTownsStations());

            this.routeService.Setup(r => r.RouteAlreadyExist(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <TimeSpan>(), It.IsAny <string>()))
            .Returns(true);

            //Act
            var result = controller.Edit(this.GetRouteFormModel(), RouteId);

            //Assert
            result.Should().BeOfType <ViewResult>();
            result.As <ViewResult>().ViewData.ModelState.Root.Errors.Should().HaveCount(1);
            result.As <ViewResult>().ViewData.ModelState.Root.Errors.Any(e => e.ErrorMessage == WebConstants.Message.CompanyRouteDuplication);
            var viewResult = result.As <ViewResult>().Model;
            var form       = viewResult.As <RouteFormModel>();

            this.AssertRouteFormModelProperties(StartStation, EndStation, form, viewResult);
            form.IsEdit.Should().BeTrue();
        }
        public void Post_Edit_WithInvalidModelStateShouldReturnEditRouteFormView()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, this.townService.Object, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.townService.Setup(t => t.GetTownsWithStations())
            .Returns(this.GetTownsStations());

            controller.ModelState.AddModelError(string.Empty, "Error");

            //Act
            var result = controller.Edit(this.GetRouteFormModel(), RouteId);

            //Assert
            result.Should().BeOfType <ViewResult>();
            var viewResult = result.As <ViewResult>().Model;
            var form       = result.As <ViewResult>().Model.As <RouteFormModel>();

            this.AssertRouteFormModelProperties(StartStation, EndStation, form, viewResult);
        }
Exemplo n.º 6
0
        public async Task EditPost_ReturnsAViewResult_WithRoute_WhenModelStateIsInvalid()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();
            var mockPointsRepo = new Mock <IPointsRepository>();

            mockPointsRepo.Setup(repo => repo.GetPoints()).Returns(Task.FromResult(GetTestPoints()));
            var controller = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);

            controller.ModelState.AddModelError("Name", "Required");
            var newRoute = new Route()
            {
                Id = 1
            };

            // Act
            var result = await controller.Edit(newRoute.Id, newRoute);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.ViewData);
            Assert.Equal(2, viewResult.ViewData.Count);
            var model = Assert.IsAssignableFrom <Route>(viewResult.ViewData.Model);

            Assert.Equal(newRoute, model);
        }
        public void Get_Edit_WhenRouteHasReservedTicketsShouldRedirectToAllRoutes()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, this.townService.Object, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.routeService.Setup(r => r.GetRouteToEdit(It.IsAny <string>(), It.IsAny <int>()))
            .Returns(this.GetRouteToEdit());

            this.townService.SetupSequence(t => t.GetTownNameByStationId(It.IsAny <int>()))
            .Returns(StartTownName)
            .Returns(EndTownName);

            this.routeService.Setup(r => r.HasReservedTickets(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(RouteId);

            //Assert
            this.AssertRedirectToCompanyRoutes(result);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.EditRouteWithTickets, StartTownName, EndTownName));
        }
        public void Get_Edit_WhenRouteDoesNotExistShouldRedirectToAllRoutes()
        {
            //Arrange
            CompanyRouteEditServiceModel route = null;
            var controller = new RoutesController(this.routeService.Object, null, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.routeService.Setup(r => r.GetRouteToEdit(It.IsAny <string>(), It.IsAny <int>()))
            .Returns(route);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(RouteId);

            //Assert
            this.AssertRedirectToCompanyRoutes(result);
            this.customMessage.Should().Be(WebConstants.Message.InvalidRoute);
        }
Exemplo n.º 9
0
        public void EditTest()
        {
            var controller = new RoutesController();
            var result     = controller.Edit(1) as ViewResult;
            var route      = result.Model as Route;

            Assert.IsTrue(route.RouteName == "Route - 1");
        }
Exemplo n.º 10
0
        public void TestGetEdit()
        {
            // Arrange
            var controller = new RoutesController(new RouteLogic(new RouteRepositoryStub()), new StationLogic(new StationRepositoryStub()));

            // ACt
            var result = (ViewResult)controller.Edit(1);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
Exemplo n.º 11
0
        public async Task EditGet_ReturnsANotFoundResult_WhenIdIsNull()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);

            // Act
            var result = await controller.Edit(null);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 12
0
        public async Task EditPost_ReturnsANotFoundResult_WhenIdIsDifferentFromRouteId()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);

            // Act
            var result = await controller.Edit(2, GetTestRoutes()[0]);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 13
0
        public void TestPostEditDBError()
        {
            // Arrange
            var controller = new RoutesController(new RouteLogic(new RouteRepositoryStub()), new StationLogic(new StationRepositoryStub()));
            var OneRoute   = new Route {
                RouteID = 0, RouteName = null
            };

            // ACt
            var result = (ViewResult)controller.Edit(OneRoute);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
Exemplo n.º 14
0
        public void TestPostEditValidationError()
        {
            // Arrange
            var controller = new RoutesController(new RouteLogic(new RouteRepositoryStub()), new StationLogic(new StationRepositoryStub()));

            controller.ViewData.ModelState.AddModelError("RouteName", "Route name should string!");
            var OneRoute = new Route();

            // ACt
            var result = (ViewResult)controller.Edit(OneRoute);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
Exemplo n.º 15
0
        public async Task EditGet_ReturnsANotFoundResult_WhenRouteReturnedIsNull()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();

            mockRoutesRepo.Setup(repo => repo.GetRouteByID(1)).Returns(Task.FromResult((Route)null));
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);

            // Act
            var result = await controller.Edit(1);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 16
0
        public void TestPostEdit()
        {
            // Arrange
            var controller = new RoutesController(new RouteLogic(new RouteRepositoryStub()), new StationLogic(new StationRepositoryStub()));
            var OneRoute   = new Route {
                RouteID = 1, RouteName = "R1"
            };

            // ACt
            var result = (RedirectToRouteResult)controller.Edit(OneRoute);

            // Assert
            Assert.AreEqual("", result.RouteName);
            Assert.AreEqual("Index", result.RouteValues.Values.First());
        }
        public void Get_Edit_WhenCompanyIsNotRouteOwnerShouldRedirectToAllRoutes()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, null, this.fixture.UserManagerMockInstance.Object, null, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(false);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(RouteId);

            //Assert
            this.AssertRedirectToCompanyRoutes(result);
            this.customMessage.Should().Be(WebConstants.Message.InvalidRoute);
        }
        public void Post_Edit_WithValidDataShouldRedirectToAllRoutes()
        {
            //Arrange
            var controller = new RoutesController(this.routeService.Object, this.townService.Object, this.fixture.UserManagerMockInstance.Object, this.companyService.Object, null);

            this.routeService.Setup(r => r.IsRouteOwner(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsApproved(It.IsAny <string>()))
            .Returns(true);

            this.companyService.Setup(c => c.IsBlocked(It.IsAny <string>()))
            .Returns(false);

            this.townService.Setup(t => t.GetTownsWithStations())
            .Returns(this.GetTownsStations());

            this.routeService.Setup(r => r.RouteAlreadyExist(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <TimeSpan>(), It.IsAny <string>()))
            .Returns(false);

            this.townService.SetupSequence(t => t.GetTownNameByStationId(It.IsAny <int>()))
            .Returns(StartTownName)
            .Returns(EndTownName);

            this.routeService.Setup(r => r.HasReservedTickets(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(false);

            this.routeService.Setup(r => r.Edit(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <TimeSpan>(), It.IsAny <TimeSpan>(), It.IsAny <BusType>(), It.IsAny <decimal>(), It.IsAny <string>()))
            .Returns(true);

            this.PrepareTempData();

            controller.TempData = this.tempData.Object;

            //Act
            var result = controller.Edit(this.GetRouteFormModel(), RouteId);

            //Assert
            this.AssertRedirectToCompanyRoutes(result);
            this.customMessage.Should().Be(string.Format(WebConstants.Message.RouteEdited, StartTownName, EndTownName));
        }
Exemplo n.º 19
0
        public async Task EditPost_ReturnsARedirectToActionResult_WhenModelStateIsValid_AndNoErrorsHappenSaving()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();

            mockRoutesRepo.Setup(repo => repo.UpdateRoute(It.IsAny <Route>())).Verifiable();
            mockRoutesRepo.Setup(repo => repo.Save()).Returns(Task.CompletedTask);
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);
            var newRoute       = GetTestRoutes()[0];

            // Act
            var result = await controller.Edit(newRoute.Id, newRoute);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
            mockRoutesRepo.Verify();
        }
Exemplo n.º 20
0
        public async Task EditPost_ReturnsANotFoundResult_WhenModelStateIsValid_AndErrorsHappenSaving_AndRouteDoesNotExist()
        {
            // Arrange
            List <IUpdateEntry> entries = new List <IUpdateEntry>
            {
                null
            };
            string message        = "Error message";
            var    mockRoutesRepo = new Mock <IRoutesRepository>();

            mockRoutesRepo.Setup(repo => repo.UpdateRoute(It.IsAny <Route>())).Throws(new DbUpdateConcurrencyException(message, entries));
            mockRoutesRepo.Setup(repo => repo.GetRouteByID(It.IsAny <int>())).Returns(Task.FromResult((Route)null));
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);
            var newRoute       = GetTestRoutes()[0];

            // Act
            var result = await controller.Edit(newRoute.Id, newRoute);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 21
0
        public async Task EditPost_ThrowsError_WhenModelStateIsValid_AndErrorsHappenSaving_AndRouteExists()
        {
            // Arrange
            List <IUpdateEntry> entries = new List <IUpdateEntry>
            {
                null
            };
            string message        = "Error message";
            var    mockRoutesRepo = new Mock <IRoutesRepository>();

            mockRoutesRepo.Setup(repo => repo.UpdateRoute(It.IsAny <Route>())).Throws(new DbUpdateConcurrencyException(message, entries));
            mockRoutesRepo.Setup(repo => repo.GetRouteByID(It.IsAny <int>())).Returns(Task.FromResult(GetTestRoutes()[0]));
            var mockPointsRepo = new Mock <IPointsRepository>();
            var controller     = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);
            var newRoute       = GetTestRoutes()[0];

            // Act
            var ex = await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => controller.Edit(newRoute.Id, newRoute));

            // Assert
            Assert.Equal(message, ex.Message);
        }
Exemplo n.º 22
0
        public async Task EditGet_ReturnsAViewResult_WithRoute()
        {
            // Arrange
            var mockRoutesRepo = new Mock <IRoutesRepository>();

            mockRoutesRepo.Setup(repo => repo.GetRouteByID(1)).Returns(Task.FromResult(GetTestRoutes()[0]));
            var mockPointsRepo = new Mock <IPointsRepository>();

            mockPointsRepo.Setup(repo => repo.GetPoints()).Returns(Task.FromResult(GetTestPoints()));
            var controller = new RoutesController(mockRoutesRepo.Object, mockPointsRepo.Object);

            // Act
            var result = await controller.Edit(1);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.ViewData);
            Assert.Equal(2, viewResult.ViewData.Count);
            var model = Assert.IsAssignableFrom <Route>(viewResult.ViewData.Model);

            Assert.Equal(GetTestRoutes()[0], model);
        }