public void Edit_sets_error_message_on_unhandled_DAL_exception_and_returns_same_view()
        {
            // setup
            var team1 = new TeamBuilder().WithName("B").Build();
            var team2 = new TeamBuilder().WithName("A").Build();

            teamRepository.Setup(p => p.GetAll()).Returns(new List <Team>()
            {
                team1, team2
            });
            var application = new ApplicationBuilder().WithTeam(team1).WithName("A").Build();

            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);
            applicationRepository.Setup(p => p.UpdateAndSubmit(It.IsAny <Application>())).Throws(new InvalidOperationException());
            var viewModel = new ApplicationManageViewModel(application);

            // act
            var controller = CreateInstance();
            var model      = (ViewResult)controller.Edit(viewModel);

            // verify
            Assert.IsNull(controller.ViewBag.SuccessMessage);
            Assert.IsNotNull(controller.ViewBag.ErrorMessage);
        }
        public void Edit_sets_error_message_on_unhandled_DAL_exception_and_returns_same_view()
        {
            // setup
            var team1 = new TeamBuilder().WithName("B").Build();
            var team2 = new TeamBuilder().WithName("A").Build();
            teamRepository.Setup(p => p.GetAll()).Returns(new List<Team>() { team1, team2 });
            var application = new ApplicationBuilder().WithTeam(team1).WithName("A").Build();
            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);
            applicationRepository.Setup(p => p.UpdateAndSubmit(It.IsAny<Application>())).Throws(new InvalidOperationException());
            var viewModel = new ApplicationManageViewModel(application);

            // act
            var controller = CreateInstance();
            var model = (ViewResult)controller.Edit(viewModel);

            // verify 
            Assert.IsNull(controller.ViewBag.SuccessMessage);
            Assert.IsNotNull(controller.ViewBag.ErrorMessage);
        }
        public void Edit_saves_application_changes_on_succesful_and_returns_to_index_with_succes_message()
        {
            // setup
            var team1 = new TeamBuilder().Build();
            var team2 = new TeamBuilder().Build();
            teamRepository.Setup(p => p.GetAll()).Returns(new List<Team>() { team1, team2 });
            var application = new ApplicationBuilder().WithTeam(team1).WithName("A").Build();
            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);

            // set up application change for name and team
            application.Name = "New name";
            application.TeamId = team2.Id;
            var viewModel = new ApplicationManageViewModel(application);

            // act
            var controller = CreateInstance();
            var model = (RedirectToRouteResult)controller.Edit(viewModel);

            // verify if all properties are passed to DAL
            applicationRepository.Verify(p => p.UpdateAndSubmit(It.Is<Application>(
                x => x.Id == application.Id
                && x.Name == application.Name
                && x.Description == application.Description
                && x.TeamId == application.TeamId)), Times.Once());

            // verify
            Assert.AreEqual("Index", model.RouteValues["Action"]);
            Assert.IsNotNull(controller.ViewBag.SuccessMessage);
            Assert.IsNull(controller.ViewBag.ErrorMessage);
        }
        public void Edit_returns_view_preloaded_with_teams_ordered_by_name_and_with_the_specified_team_selected()
        {
            // setup
            var team1 = new TeamBuilder().WithName("B").Build();
            var team2 = new TeamBuilder().WithName("A").Build();
            teamRepository.Setup(p => p.GetAll()).Returns(new List<Team>() { team1, team2 });
            var application = new ApplicationBuilder().WithTeam(team1).WithName("A").Build();
            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);

            // act
            var controller = CreateInstance();
            ViewResult model = controller.Edit(application.Id) as ViewResult;
            SelectList result = (SelectList)model.ViewBag.TeamId;

            // verify if correct application was returned
            Assert.AreEqual(application.Name, ((ApplicationManageViewModel)model.Model).Name);
            Assert.AreEqual(application.Id, ((ApplicationManageViewModel)model.Model).Id);

            // verify teams 
            Assert.AreEqual(2, result.Items.OfType<Team>().Count());
            Assert.AreEqual(application.TeamId, result.SelectedValue); // check if team was selected
            Assert.AreEqual("A", result.Items.OfType<Team>().First().Name);
            Assert.AreEqual("B", result.Items.OfType<Team>().Last().Name);
        }