public void RadioButtonListRendersOK() { // Arrange // 1 Set up an intance of the viewModelit works with SurveyResponsesViewModel responses = new SurveyResponsesViewModel(); // Populate with data responses.Answer = "5"; responses.LikertScaleNumber = 1L; responses.QId_SeqNo = "1_1"; responses.Text = "Strongly Disagree"; // var mockhttpContext = new Mock<HttpContextBase>(); // not used in helper so don't need it. var mockViewContext = new ViewContext(); var mockViewDataContainer = new FakeViewDataContainer(); // 2 instantiate an HtmlHelper // NB! NB! NB! NB! NB! NB! NB! // This is a STRONGLY TYPED Html Helper, as therefore we need to instantiate an html helper with a model. // NB! NB! NB! NB! NB! NB! NB! HtmlHelper<SurveyResponsesViewModel> helper = new HtmlHelper<SurveyResponsesViewModel>(mockViewContext, mockViewDataContainer); // Act MvcHtmlString result = HtmlHelperExtensions.RadioButtonListFor(helper, (r => responses.LikertScaleNumber) , responses.LikertScaleNumber, responses.QId_SeqNo, responses.Answer, null); Assert.IsNotNull(result); Assert.AreEqual(true, result.ToHtmlString().Contains("input"), "Expected input. tag"); Assert.AreEqual(true, result.ToHtmlString().Contains("name"), "Expected name attribute"); Assert.IsFalse(result.ToHtmlString().Contains("checked"), "Should not have been a checked attribute"); }
public TakeSurveyViewModel Map(TakeSurveyViewModel responses) { Survey survey = _surveyRepository.GetSurvey(responses.SurveyId); TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Map the survey to the survey view model. viewModel.SurveyId = survey.SurveyId; viewModel.Title = survey.Title; viewModel.UserName = survey.User.UserName; viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate); viewModel.CategoryDescription = survey.Category.Description; // Map the questions to the view model viewModel.Questions = new List<SurveyQuestionsViewModel>(); //foreach (Core.Model.Question q in survey.Questions) foreach (SurveyQuestionsViewModel q in responses.Questions) { // Get the question and available responses from the repository. Core.Model.Question questionInfo = _questionRepository.GetQuestion(Convert.ToInt64(q.QId_SeqNo.Split('_')[0])); SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel(); questionViewModel.QId_SeqNo = q.QId_SeqNo; questionViewModel.SequenceNumber = questionInfo.SequenceNumber; questionViewModel.Text = questionInfo.Text; questionViewModel.Answer = q.Answer; // Map the responses to the question questionViewModel.Responses = new List<SurveyResponsesViewModel>(); // this causes a DBContext Disposed error if it's visited a 2nd time. foreach (Core.Model.AvailableResponse r in questionInfo.AvailableResponses) { SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel(); responsesViewModel.QId_SeqNo = q.QId_SeqNo; responsesViewModel.Answer = q.Answer.ToString(); responsesViewModel.LikertScaleNumber = r.LikertScaleNumber; responsesViewModel.Text = r.Text; questionViewModel.Responses.Add(responsesViewModel); } viewModel.Questions.Add(questionViewModel); } return viewModel; }
/// <summary> /// Map the Survey to the TakeSurveyViewModel. /// </summary> /// <remarks> /// This mapping is done explicitly as the target structure is quite complex /// and the amount of exceptions required if AutoMapper were used would not /// have a significant effect on the amount of code. /// Additionally, the top level represents a single survey, but the /// lower levels represent collections of objects. /// </remarks> public override void OnActionExecuted(ActionExecutedContext filterContext) { Survey survey = (Survey) filterContext.Controller.ViewData.Model; TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Map the survey to the survey view model. viewModel.SurveyId = survey.SurveyId; viewModel.Title = survey.Title; viewModel.UserName = survey.User.UserName; viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate); viewModel.CategoryDescription = survey.Category.Description; // Map the questions to the view model viewModel.Questions = new List<SurveyQuestionsViewModel>(); foreach (Core.Model.Question q in survey.Questions) { SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel(); questionViewModel.QId_SeqNo = q.QuestionId.ToString() + "_" + q.SequenceNumber.ToString(); questionViewModel.SequenceNumber = q.SequenceNumber; questionViewModel.Text = q.Text; // Map the responses to the question questionViewModel.Responses = new List<SurveyResponsesViewModel>(); foreach (Core.Model.AvailableResponse r in q.AvailableResponses) { SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel(); responsesViewModel.QId_SeqNo = questionViewModel.QId_SeqNo; responsesViewModel.LikertScaleNumber = r.LikertScaleNumber; responsesViewModel.Text = r.Text; questionViewModel.Responses.Add(responsesViewModel); } viewModel.Questions.Add(questionViewModel); } filterContext.Controller.ViewData.Model = viewModel; }