public void GivenViewModelWithInvalidStudentId_WhenAddProviders_ThenThrowException()
        {
            AddStudentApprovalModel viewModel = new AddStudentApprovalModel {
                StudentId = 100
            };

            Target.ExpectException <EntityNotFoundException>(() => Target.AddProviders(viewModel));
        }
        public void GivenViewModelWithValidStudentId_AndSecondOfTwoSelectedProvidersIsInvalid_WhenAddProviders_ThenThrowException()
        {
            AddStudentApprovalModel viewModel = new AddStudentApprovalModel {
                StudentId = 1, ProvidersToAdd = new[] { 1, 100 }
            };

            Target.ExpectException <EntityNotFoundException>(() => Target.AddProviders(viewModel));
        }
Exemplo n.º 3
0
        public AddStudentApprovalModel GenerateAddStudentApprovalViewModel(int id)
        {
            var viewModel = new AddStudentApprovalModel();

            viewModel.StudentId = id;
            PopulateViewModelLists(viewModel);
            return(viewModel);
        }
        public void GivenViewModelWithValidStudentId_AndValidListOfProviders_WhenAddProviders_ThenSaveChanges()
        {
            var viewModel = new AddStudentApprovalModel {
                StudentId = 1, ProvidersToAdd = new[] { 1, 2 }
            };

            Target.AddProviders(viewModel);

            Repositories.MockRepositoryContainer.AssertWasCalled(m => m.Save());
        }
        public void GivenLogicManagerGeneratesViewModel_WhenGetAddProviders_ThenViewResultContainsViewModel()
        {
            AddStudentApprovalModel expected = new AddStudentApprovalModel();

            MockLogicManager.Expect(m => m.GenerateAddStudentApprovalViewModel(1)).Return(expected);

            PartialViewResult actual = Target.AddProviders(1) as PartialViewResult;

            actual.AssertGetViewModel(expected);
        }
        public void GivenModelErrors_WhenPostAddProviders_ThenViewModelGetsPopulated()
        {
            AddStudentApprovalModel expected = new AddStudentApprovalModel();

            Target.ModelState.AddModelError("whatever", "this is an error");

            PartialViewResult result = Target.AddProviders(expected) as PartialViewResult;

            MockLogicManager.AssertWasCalled(m => m.PopulateViewModelLists(expected));
        }
        public void GivenModelErrors_WhenPostAddProviders_ThenViewResultReturnedWithViewModel()
        {
            AddStudentApprovalModel expected = new AddStudentApprovalModel();

            Target.ModelState.AddModelError("whatever", "this is an error");

            PartialViewResult result = Target.AddProviders(expected) as PartialViewResult;

            result.AssertGetViewModel(expected);
        }
Exemplo n.º 8
0
 public ActionResult AddProviders(AddStudentApprovalModel viewModel)
 {
     if (ModelState.IsValid)
     {
         LogicManager.AddProviders(viewModel);
         return(Json(true));
     }
     LogicManager.PopulateViewModelLists(viewModel);
     return(PartialView(viewModel));
 }
        public void GivenValidStudentId_WhenGenerateAddStudentApprovalViewModel_ThenViewModelContainsStudentId()
        {
            var     expectedStudentId = 3;
            Student toFind            = Repositories.MockStudentRepository.Items.Where(s => s.Id == expectedStudentId).Single();

            AddStudentApprovalModel result = Target.GenerateAddStudentApprovalViewModel(expectedStudentId);

            int actual = result.StudentId;

            Assert.IsNotNull(actual);
            Assert.AreEqual(expectedStudentId, actual);
        }
        public void GivenValidStudentId_WhenGenerateAddStudentApprovalViewModel_ThenViewModelContainsListOfProvidersNotAlreadyAssociated()
        {
            Student toFind = Repositories.MockStudentRepository.Items.Where(s => s.Id == 3).Single();

            toFind.ApprovedProviders.Add(Repositories.MockProviderRepository.Items.Where(p => p.Id == 2).Single());
            IEnumerable <Provider> expectedProviders = Repositories.MockProviderRepository.Items.Where(p => p.Id != 2);

            AddStudentApprovalModel result = Target.GenerateAddStudentApprovalViewModel(3);

            MultiSelectList actual = result.Providers;

            Assert.IsNotNull(actual);
            Assert.AreEqual("Id", actual.DataValueField);
            Assert.AreEqual("Name", actual.DataTextField);
            CollectionAssert.AreEqual(expectedProviders.ToList(), actual.Items.Cast <Provider>().ToList());
        }
Exemplo n.º 11
0
        public void PopulateViewModelLists(AddStudentApprovalModel viewModel)
        {
            var student = StudentRepository.Items.Include(s => s.ApprovedProviders).SingleOrDefault(s => s.Id == viewModel.StudentId);

            if (student == null)
            {
                throw new EntityNotFoundException("Student with the specified identifier was not found.");
            }
            IEnumerable <Provider> providersNotYetApproved = ProviderRepository.Items;

            foreach (Provider studentProvider in student.ApprovedProviders)
            {
                providersNotYetApproved = providersNotYetApproved.Where(p => p.Id != studentProvider.Id);
            }
            viewModel.Providers = new MultiSelectList(providersNotYetApproved, "Id", "Name");
        }
        public void GivenViewModelWithValidStudentId_AndValidListOfProviders_WhenAddProviders_ThenAddLinkCalledBetweenStudentAndAllSelectedProviders()
        {
            var studentId = 1;

            int[]   providerIds     = new[] { 1, 2 };
            Student expectedStudent = Repositories.MockStudentRepository.Items.Where(s => s.Id == studentId).Single();
            IEnumerable <Provider> expectedProviders = Repositories.MockProviderRepository.Items.Where(p => providerIds.Contains(p.Id));
            var viewModel = new AddStudentApprovalModel {
                StudentId = studentId, ProvidersToAdd = providerIds
            };

            Target.AddProviders(viewModel);

            foreach (Provider expectedProvider in expectedProviders)
            {
                Repositories.MockStudentRepository.AssertWasCalled(m => m.AddLink(expectedStudent, expectedProvider));
            }
        }
Exemplo n.º 13
0
        public void AddProviders(AddStudentApprovalModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }
            var student = StudentRepository.Items.SingleOrDefault(s => s.Id == viewModel.StudentId);

            if (student == null)
            {
                throw new EntityNotFoundException("Student with specified identifier was not found.");
            }
            var selectedProviders = ProviderRepository.Items.Where(p => viewModel.ProvidersToAdd.Contains(p.Id)).ToList();

            if (selectedProviders.Count < viewModel.ProvidersToAdd.Count())
            {
                throw new EntityNotFoundException("At least one provider selected cannot be associated with the student because it was not found using the given identifier.");
            }
            foreach (Provider provider in selectedProviders)
            {
                StudentRepository.AddLink(student, provider);
            }
            RepositoryContainer.Save();
        }
        public void GivenValidStudentId_WhenGenerateAddStudentApprovalViewModel_ThenViewModelReturned()
        {
            AddStudentApprovalModel actual = Target.GenerateAddStudentApprovalViewModel(3);

            Assert.IsNotNull(actual);
        }
Exemplo n.º 15
0
        public ActionResult AddProviders(int id)
        {
            AddStudentApprovalModel viewModel = LogicManager.GenerateAddStudentApprovalViewModel(id);

            return(PartialView(viewModel));
        }