コード例 #1
0
        public void ToDalEntity_converts_model_properties_to_DAL_entity()
        {
            // setup
            var application = new ApplicationBuilder().WithName("Name").WithDefaultTeam().Build();
            var model = new ApplicationManageViewModel(application);

            // act
            var result = model.ToDalEntity();

            // verify
            Assert.AreEqual(application.Id, result.Id);
            Assert.AreEqual(application.Name, result.Name);
            Assert.AreEqual(application.Description, result.Description);
            Assert.AreEqual(application.TeamId, result.TeamId);
        }
コード例 #2
0
        public ActionResult Create(ApplicationManageViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var application = model.ToDalEntity();
                    applicationRepository.InsertAndSubmit(application);

                    base.SetSuccessMessage("The application [{0}] has been created.", application.Name);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    base.SetErrorMessage("Whoops! Couldn't create the new application. The error was [{0}]", ex.Message);
                }
            }

            LoadTeamsInViewData();
            return View(model);
        }
コード例 #3
0
        public ActionResult Edit(ApplicationManageViewModel model)
        {
            if (ModelState.IsValid)
            {
                var application = applicationRepository.GetById(model.Id);
                if (application == null) { throw new ArgumentException(string.Format("Application with Id [{0}] does not exist", model.Id)); }

                try
                {
                    model.ToDalEntity(application);
                    applicationRepository.UpdateAndSubmit(application);

                    base.SetSuccessMessage("The application [{0}] has been updated.", application.Name);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    base.SetErrorMessage("Whoops! Couldn't update the application. The error was [{0}]", ex.Message);
                }
            }

            LoadTeamsInViewData();
            return View(model);
        }
コード例 #4
0
        public void Create_saves_new_application_on_succesful_submit_and_returns_to_index_with_success_message()
        {
            // setup
            var application = new ApplicationBuilder().WithName("New app").WithDefaultTeam().Build();
            var viewModel = new ApplicationManageViewModel(application);

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

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

            // verify
            Assert.IsTrue(controller.ViewData.ModelState.IsValid);
            Assert.IsNotNull(controller.ViewBag.SuccessMessage);
            Assert.IsNull(controller.ViewBag.ErrorMessage);
            Assert.AreEqual("Index", model.RouteValues["Action"]);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public void Create_sets_error_message_on_unhandled_DAL_exception_and_returns_same_view()
        {
            // setup
            var application = new ApplicationBuilder().WithDefaultTeam().Build();
            var viewModel = new ApplicationManageViewModel(application);
            applicationRepository.Setup(p => p.InsertAndSubmit(It.IsAny<Application>())).Throws(new InvalidOperationException());

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

            // verify
            Assert.IsNotNull(controller.ViewBag.ErrorMessage);
            Assert.IsNull(controller.ViewBag.SuccessMessage);
        }