public async Task ThenRedirectsToConfirmProvider(
            SelectProviderViewModel viewModel,
            long providerId,
            ValidationResult validationResult,
            [Frozen] Mock <IValidator <SelectProviderViewModel> > mockValidator,
            [Frozen] Mock <ICommitmentsApiClient> mockApiClient,
            [Frozen] Mock <IModelMapper> mockMapper,
            GetProviderResponse apiResponse,
            ConfirmProviderRequest confirmProviderRequest,
            [Greedy] CohortController controller)
        {
            var actionName = "ConfirmProvider";

            viewModel.ProviderId = providerId.ToString();
            mockValidator
            .Setup(x => x.Validate(viewModel))
            .Returns(validationResult);
            mockApiClient
            .Setup(x => x.GetProvider(long.Parse(viewModel.ProviderId), CancellationToken.None))
            .ReturnsAsync(apiResponse);
            mockMapper
            .Setup(x => x.Map <ConfirmProviderRequest>(viewModel))
            .ReturnsAsync(confirmProviderRequest);

            var result = await controller.SelectProvider(viewModel) as RedirectToActionResult;

            Assert.NotNull(result);
            Assert.AreEqual(actionName, result.ActionName);
        }
        public async Task ThenMapsTheRequestToViewModel(
            SelectProviderRequest request,
            SelectProviderViewModel viewModel,
            [Frozen] Mock <IModelMapper> mockMapper,
            [Greedy] CohortController controller)
        {
            await controller.SelectProvider(request);

            mockMapper.Verify(x => x.Map <SelectProviderViewModel>(It.IsAny <SelectProviderRequest>()), Times.Once);
        }
        public async Task ThenCallsApi(
            SelectProviderViewModel viewModel,
            long providerId,
            [Frozen] Mock <ICommitmentsApiClient> mockApiClient,
            GetProviderResponse apiResponse,
            [Greedy] CohortController controller)
        {
            viewModel.ProviderId = providerId.ToString();

            await controller.SelectProvider(viewModel);

            mockApiClient.Verify(x => x.GetProvider(providerId, CancellationToken.None), Times.Once);
        }
        public async Task ThenReturnsView(
            SelectProviderRequest request,
            SelectProviderViewModel viewModel,
            [Frozen] Mock <IModelMapper> mockMapper,
            [Greedy] CohortController controller)
        {
            mockMapper
            .Setup(mapper => mapper.Map <SelectProviderViewModel>(request))
            .ReturnsAsync(viewModel);

            var result = await controller.SelectProvider(request) as ViewResult;

            Assert.Null(result.ViewName);
            Assert.AreSame(viewModel, result.Model);
        }
        public async Task AndUnexpectedExceptionThrown_ThenReturnsErrorView(
            SelectProviderViewModel viewModel,
            long providerId,
            [Frozen] Mock <ICommitmentsApiClient> mockApiClient,
            GetProviderResponse apiResponse,
            HttpResponseMessage error,
            [Greedy] CohortController controller)
        {
            error.StatusCode     = HttpStatusCode.NetworkAuthenticationRequired;
            viewModel.ProviderId = providerId.ToString();
            mockApiClient
            .Setup(x => x.GetProvider(providerId, CancellationToken.None))
            .ThrowsAsync(new RestHttpClientException(error, error.ReasonPhrase));

            var result = await controller.SelectProvider(viewModel) as RedirectToActionResult;

            Assert.NotNull(result);
            Assert.AreEqual("Error", result.ActionName);
            Assert.AreEqual("Error", result.ControllerName);
        }
        public async Task ThenMapsConfirmProviderRequest(
            SelectProviderViewModel viewModel,
            long providerId,
            ValidationResult validationResult,
            [Frozen] Mock <IValidator <SelectProviderViewModel> > mockValidator,
            [Frozen] Mock <ICommitmentsApiClient> mockApiClient,
            [Frozen] Mock <IModelMapper> mockMapper,
            GetProviderResponse apiResponse,
            [Greedy] CohortController controller)
        {
            viewModel.ProviderId = providerId.ToString();
            mockValidator
            .Setup(x => x.Validate(viewModel))
            .Returns(validationResult);
            mockApiClient
            .Setup(x => x.GetProvider(long.Parse(viewModel.ProviderId), CancellationToken.None))
            .ReturnsAsync(apiResponse);

            await controller.SelectProvider(viewModel);

            mockMapper.Verify(x => x.Map <ConfirmProviderRequest>(viewModel), Times.Once);
        }
        public async Task AndApiThrowsNotFoundException_ThenReturnsRedirectResult(
            SelectProviderViewModel viewModel,
            long providerId,
            [Frozen] Mock <ICommitmentsApiClient> mockApiClient,
            HttpResponseMessage error,
            [Frozen] Mock <IModelMapper> modelMapper,
            [Greedy] CohortController controller)
        {
            modelMapper.Setup(x => x.Map <SelectProviderRequest>(viewModel))
            .ReturnsAsync(new SelectProviderRequest());

            error.StatusCode     = HttpStatusCode.NotFound;
            viewModel.ProviderId = providerId.ToString();
            mockApiClient
            .Setup(x => x.GetProvider(providerId, CancellationToken.None))
            .ThrowsAsync(new RestHttpClientException(error, error.ReasonPhrase));

            var result = await controller.SelectProvider(viewModel) as RedirectToActionResult;

            Assert.AreEqual(nameof(CohortController.SelectProvider), result.ActionName);
            Assert.False(controller.ModelState.IsValid);
        }