public async Task WhenFindAddressPostedAndFormIsValid_ThenAddressServiceCalled() { var controller = new EditYourDetailsController(_compositeSettings, _authService, _addressSearchService, _dssReader, _dssWriter, _documentService, _config) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; _dssReader.GetCustomerData(Arg.Any <string>()).ReturnsForAnyArgs(new Customer { Contact = new Contact { EmailAddress = "test" } }); _addressSearchService.GetAddresses(Arg.Any <string>()).ReturnsForAnyArgs(new List <PostalAddressModel> { new PostalAddressModel { Line1 = "test" } }); var viewModel = GetViewModel(); viewModel.Identity.PersonalDetails.HomePostCode = "lg23hg"; var result = await controller.Body(viewModel, new FormCollection(new Dictionary <string, StringValues> { { "findAddress", "findAddress" } })) as ViewResult; result.Should().NotBeNull(); ((EditDetailsCompositeViewModel)result.ViewData.Model).Items.Any().Should().BeTrue(); }
private async Task FindAddress(EditDetailsCompositeViewModel viewModel) { var postalCode = viewModel.Identity.PersonalDetails.HomePostCode?.Trim(); if (string.IsNullOrWhiteSpace(postalCode) || !ServiceCommon.IsValidUkPostcode(postalCode)) { const string errorMessage = "Enter a valid postal code"; ModelState.AddModelError("Identity.PersonalDetails.HomePostCode", errorMessage); } else { bool isFindAddressServiceOnError = false; try { var addresses = (await _addressSearchService.GetAddresses(postalCode)).ToList(); if (addresses.Any()) { viewModel.Items = addresses.ToList(); } } catch { viewModel.Identity.PersonalDetails.FindAddressServiceResult = ErrorMessageServiceUnavailable; isFindAddressServiceOnError = true; } if (viewModel.Items == null || viewModel.Items.Count == 0) { viewModel.Items = new List <PostalAddressModel>() { new PostalAddressModel() { Error = 0 } }; if (!isFindAddressServiceOnError) { string noResultsMessage = "We do not have any address matching this postcode. Please enter your address details in the boxes provided."; viewModel.Identity.PersonalDetails.FindAddressServiceResult = noResultsMessage; } } } }