Exemple #1
0
        public async Task WhenAnsweredRedirectsToCorrectNextView(bool isComplete, string expectedRedirect)
        {
            var assessmentType        = "short";
            var jobCategoryName       = "sales";
            var questionNumberReal    = 1;
            var questionNumberCounter = 1;
            var answer         = "answer";
            var answerResponse = new PostAnswerResponse()
            {
                IsComplete = isComplete, IsSuccess = true
            };
            var viewModel = new FilterQuestionPostRequestViewModel()
            {
                AssessmentType        = assessmentType,
                JobCategoryName       = jobCategoryName,
                Answer                = answer,
                QuestionNumberReal    = questionNumberReal,
                QuestionNumberCounter = questionNumberCounter,
            };

            A.CallTo(() => sessionService.HasValidSession()).Returns(true);

            A.CallTo(() => assessmentService.AnswerQuestion(jobCategoryName, questionNumberReal, questionNumberReal, answer)).Returns(answerResponse);

            var actionResponse = await controller.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <RedirectResult>(actionResponse);
            var redirectResult = actionResponse as RedirectResult;

            Assert.Equal($"~/{RouteName.Prefix}/{assessmentType}/filterquestions/{jobCategoryName}/{expectedRedirect}", redirectResult.Url);
        }
        public async Task <IActionResult> Index(QuestionPostRequestViewModel requestViewModel)
        {
            if (requestViewModel == null)
            {
                return(BadRequest());
            }

            var hasSessionId = await HasSessionId().ConfigureAwait(false);

            if (!hasSessionId)
            {
                return(RedirectToRoot());
            }

            var question = await GetQuestion(requestViewModel.AssessmentType, requestViewModel.QuestionNumber).ConfigureAwait(false);

            if (question == null)
            {
                return(BadRequest());
            }

            var result = mapper.Map <QuestionGetResponseViewModel>(question);

            if (!ModelState.IsValid)
            {
                return(View(result));
            }

            var answerResponse = await apiService.AnswerQuestion(requestViewModel.AssessmentType, requestViewModel.QuestionNumber, requestViewModel.QuestionNumber, requestViewModel.Answer).ConfigureAwait(false);

            if (answerResponse.IsSuccess)
            {
                if (answerResponse.IsComplete)
                {
                    return(RedirectTo("assessment/complete"));
                }
                else
                {
                    var assessmentTypeName = GetAssessmentTypeName(requestViewModel.AssessmentType);
                    return(RedirectTo($"assessment/{assessmentTypeName}/{answerResponse.NextQuestionNumber}"));
                }
            }
            else
            {
                ModelState.AddModelError("Answer", "Failed to record answer");
                return(View(result));
            }
        }
        public async Task <IActionResult> Index(FilterQuestionPostRequestViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

            var hasSessionId = await HasSessionId().ConfigureAwait(false);

            if (!hasSessionId)
            {
                return(RedirectToRoot());
            }

            if (!ModelState.IsValid)
            {
                var response = await GetQuestion(viewModel.JobCategoryName, viewModel.QuestionNumberCounter).ConfigureAwait(false);

                return(View(response));
            }

            var answerResponse = await apiService.AnswerQuestion(viewModel.JobCategoryName, viewModel.QuestionNumberReal, viewModel.QuestionNumberCounter, viewModel.Answer).ConfigureAwait(false);

            if (answerResponse == null)
            {
                return(BadRequest());
            }

            if (!answerResponse.IsSuccess)
            {
                ModelState.AddModelError("Answer", "Unable to register answer");
                var response = await GetQuestion(viewModel.JobCategoryName, viewModel.QuestionNumberCounter).ConfigureAwait(false);

                return(View(response));
            }

            if (answerResponse.IsComplete)
            {
                return(RedirectTo($"{viewModel.AssessmentType}/filterquestions/{viewModel.JobCategoryName}/complete"));
            }

            return(RedirectTo($"{viewModel.AssessmentType}/filterquestions/{viewModel.JobCategoryName}/{viewModel.QuestionNumberCounter + 1}"));
        }
        public async Task AnswerQuestionReturnsValidResponse()
        {
            var sessionId        = "session1";
            var assessmentType   = "at1";
            var questionResponse = new GetQuestionResponse()
            {
                QuestionNumber = 1, QuestionSetVersion = $"{assessmentType}-v1"
            };
            var answerRequest = new PostAnswerRequest()
            {
                QuestionId = $"{assessmentType}-v1-1", SelectedOption = "2"
            };
            var answerResponse = A.Fake <PostAnswerResponse>();

            A.CallTo(() => sessionService.GetSessionId()).Returns(sessionId);
            A.CallTo(() => assessmentApiService.GetQuestion(sessionId, assessmentType, questionResponse.QuestionNumber)).Returns(questionResponse);
            A.CallTo(() => assessmentApiService.AnswerQuestion(sessionId, answerRequest)).Returns(answerResponse);

            var response = await assessmentService.AnswerQuestion(assessmentType, questionResponse.QuestionNumber, questionResponse.QuestionNumber, answerRequest.SelectedOption);

            Assert.Equal(answerResponse.IsSuccess, response.IsSuccess);
        }