public void AndTalkFoundButQuestionnaireIsEmpty_ThenIndexRedirectsToTalkNotFound()
        {
            A.CallTo(() => _talksApi.GetFeedbackFormByEasyAccessKey("12345"))
                .Returns(new FeedbackForm());

            var controller = new FeedbackController(_talksApi);
            var actionResult = controller.Index("12345");
            var viewResult = (RedirectToActionResult)actionResult;

            Assert.That(viewResult, Is.Not.Null);
            Assert.That(viewResult.ActionName, Is.EqualTo("TalkNotFound"));
            Assert.That(viewResult.ControllerName, Is.EqualTo("Home"));
            Assert.That(viewResult.RouteValues["EasyAccessKey"], Is.EqualTo("12345"));
        }
        public void AndFeedbackIsPosted_ThenViewModelIsValidatedCorrectly()
        {
            var expectedMessage = "Please provide an answer to this question";

            var controller = new FeedbackController(_talksApi);

            var validationErrors = CheckForValidationErrors(controller, _viewModel);

            var actionResult = (ViewResult)controller.Index(_viewModel);
            var modelState = controller.ModelState;

            Assert.That(validationErrors.Count, Is.EqualTo(1));
            Assert.That(validationErrors[0].ErrorMessage, Is.EqualTo(expectedMessage));

            Assert.That(actionResult.ViewName, Is.EqualTo("Index"));
        }
        public void AndTalkIsNotFound_ThenIndexRedirectsToTalkNotFound()
        {
            var notFoundKey = "not the key you are looking for";

            A.CallTo(() => _talksApi.GetFeedbackFormByEasyAccessKey(notFoundKey))
                .Returns(null);

            var controller = new FeedbackController(_talksApi);
            var actionResult = controller.Index(notFoundKey);
            var viewResult = (RedirectToActionResult)actionResult;

            Assert.That(viewResult, Is.Not.Null);
            Assert.That(viewResult.ActionName, Is.EqualTo("TalkNotFound"));
            Assert.That(viewResult.ControllerName, Is.EqualTo("Home"));
            Assert.That(viewResult.RouteValues["EasyAccessKey"], Is.EqualTo(notFoundKey));
        }
        public void AndTalksApiCannotSaveReview_ThenShouldRedirectWithError()
        {
            A.CallTo(() => _talksApi.PostFeedbackForm(
                    _easyAccessKey,
                    A<FeedbackResponse>.Ignored))
             .Returns(new HttpResponseMessage { StatusCode = HttpStatusCode.Conflict, ReasonPhrase = "Couldn't Find Talk" });

            var controller = new FeedbackController(_talksApi);

            var viewResult = (RedirectToActionResult)controller.Index(_viewModel);
            var errorMessage = (string)viewResult.RouteValues["ErrorMessage"];

            Assert.That(viewResult, Is.Not.Null);
            Assert.That(viewResult.ActionName, Is.EqualTo("Index"));
            Assert.That(viewResult.ControllerName, Is.EqualTo("Feedback"));
            Assert.That(errorMessage, Is.EqualTo("Couldn't Find Talk"));
        }
        public void AndFeedbackIsSuccessfullyPosted_ThenShouldRedirectSuccessfully()
        {
            A.CallTo(() => _talksApi.PostFeedbackForm(
                    _easyAccessKey,
                    A<FeedbackResponse>.Ignored))
             .Returns(new HttpResponseMessage { StatusCode = HttpStatusCode.Created});

            var controller = new FeedbackController(_talksApi);

            var viewResult = (ViewResult)controller.Index(_viewModel);

            Assert.That(viewResult, Is.Not.Null);
            Assert.That(viewResult.ViewName, Is.EqualTo("_feedbackSavedSuccessfully"));
            A.CallTo(() 
                => _talksApi.PostFeedbackForm(
                    _easyAccessKey, 
                    A<FeedbackResponse>.Ignored)).MustHaveHappened();
        }
        public void AndTalkIsFound_ThenIndexShouldReturnView()
        {
            A.CallTo(() => _talksApi.GetFeedbackFormByEasyAccessKey("12345"))
                .Returns(new FeedbackForm() {
                    Questionnaire = new List<Question>()
                        {
                            new Question()
                        }
                });

            var controller = new FeedbackController(_talksApi);
            var actionResult = controller.Index("12345");
            var viewResult = (ViewResult)actionResult;

            Assert.That(viewResult, Is.Not.Null);
            Assert.That(viewResult.ViewName, Is.EqualTo("Index"));
            Assert.That(viewResult.Model, Is.TypeOf<FeedbackFormViewModel>());
        }
        private static IList<ValidationResult> CheckForValidationErrors(FeedbackController controller, FeedbackFormViewModel model)
        {
            var validationErrors = ViewModelValidation.Validate(model);

            if (validationErrors.Count > 0)
            {
                controller.ViewData.ModelState.AddModelError(nameof(model.TalkId), validationErrors[0].ErrorMessage);
            }

            return validationErrors;
        }