public async Task SubmitInvalidOrder_CallsErrorDialog() { bool successDialogCalled = false; bool errorDialogCalled = false; var navigationService = new MockNavigationService(); var accountService = new MockAccountService() { VerifySavedCredentialsAsyncDelegate = () => Task.FromResult<UserInfo>(new UserInfo()) }; var orderService = new MockOrderService() { // the order is invalid, it cannot be processed ProcessOrderAsyncDelegate = (o) => { var modelValidationResult = new ModelValidationResult(); modelValidationResult.ModelState.Add("someKey", new List<string>() { "the value of someKey is invalid" }); throw new ModelValidationException(modelValidationResult); } }; var resourcesService = new MockResourceLoader() { GetStringDelegate = (key) => key }; var alertService = new MockAlertMessageService() { ShowAsyncDelegate = (dialogTitle, dialogMessage) => { successDialogCalled = dialogTitle.ToLower().Contains("purchased"); errorDialogCalled = !successDialogCalled; return Task.FromResult(successDialogCalled); } }; var target = new CheckoutSummaryPageViewModel(navigationService, orderService, null, null, null, null, accountService, resourcesService, alertService, null); await target.SubmitCommand.Execute(); Assert.IsFalse(successDialogCalled); Assert.IsTrue(errorDialogCalled); }
private void DisplayOrderErrorMessages(ModelValidationResult validationResult) { var shippingAddressErrors = new Dictionary<string, ReadOnlyCollection<string>>(); var billingAddressErrors = new Dictionary<string, ReadOnlyCollection<string>>(); var paymentMethodErrors = new Dictionary<string, ReadOnlyCollection<string>>(); // Property keys of the form. Format: order.{ShippingAddress/BillingAddress/PaymentMethod}.{Property} foreach (var propkey in validationResult.ModelState.Keys) { string orderPropAndEntityProp = propkey.Substring(propkey.IndexOf('.') + 1); // strip off order. prefix string orderProperty = orderPropAndEntityProp.Substring(0, orderPropAndEntityProp.IndexOf('.') + 1); string entityProperty = orderPropAndEntityProp.Substring(orderProperty.IndexOf('.') + 1); if (orderProperty.ToLower().Contains("shipping")) { shippingAddressErrors.Add(entityProperty, new ReadOnlyCollection<string>(validationResult.ModelState[propkey])); } if (orderProperty.ToLower().Contains("billing") && !UseSameAddressAsShipping) { billingAddressErrors.Add(entityProperty, new ReadOnlyCollection<string>(validationResult.ModelState[propkey])); } if (orderProperty.ToLower().Contains("payment")) { paymentMethodErrors.Add(entityProperty, new ReadOnlyCollection<string>(validationResult.ModelState[propkey])); } } if (shippingAddressErrors.Count > 0) { _shippingAddressViewModel.Address.Errors.SetAllErrors(shippingAddressErrors); } if (billingAddressErrors.Count > 0) { _billingAddressViewModel.Address.Errors.SetAllErrors(billingAddressErrors); } if (paymentMethodErrors.Count > 0) { _paymentMethodViewModel.PaymentMethod.Errors.SetAllErrors(paymentMethodErrors); } }
public ModelValidationException(ModelValidationResult validationResult) { ValidationResult = validationResult; }
private void DisplayValidationErrors(ModelValidationResult modelValidationResults) { var errors = new Dictionary<string, ReadOnlyCollection<string>>(); // Property keys format: address.{Propertyname} foreach (var propkey in modelValidationResults.ModelState.Keys) { string propertyName = propkey.Substring(propkey.IndexOf('.') + 1); // strip off order. prefix errors.Add(propertyName, new ReadOnlyCollection<string>(modelValidationResults.ModelState[propkey])); } if (errors.Count > 0) { PaymentMethodViewModel.PaymentMethod.Errors.SetAllErrors(errors); } }
public void ProcessFormAsync_WithServerValidationError_ShowsMessage() { var shippingAddressPageViewModel = new MockShippingAddressPageViewModel() { ValidateFormDelegate = () => true, ProcessFormAsyncDelegate = () => Task.Delay(0), Address = new Address() }; var billingAddressPageViewModel = new MockBillingAddressPageViewModel() { ValidateFormDelegate = () => true, ProcessFormAsyncDelegate = () => Task.Delay(0), Address = new Address() }; var paymentMethodPageViewModel = new MockPaymentMethodPageViewModel() { ValidateFormDelegate = () => true, ProcessFormAsyncDelegate = async () => await Task.Delay(0), PaymentMethod = new PaymentMethod() }; var accountService = new MockAccountService() { VerifyUserAuthenticationAsyncDelegate = () => Task.FromResult(new UserInfo()), SignedInUser = new UserInfo() }; var shoppingCartRepository = new MockShoppingCartRepository() { GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(null)) }; var orderRepository = new MockOrderRepository() { CreateBasicOrderAsyncDelegate = (s, cart, arg3, arg4, arg5) => { var result = new ModelValidationResult(); result.ModelState.Add("order.ShippingAddress.ZipCode", new List<string>{"Validation Message"}); throw new ModelValidationException(result); } }; var target = new CheckoutHubPageViewModel(new MockNavigationService(), accountService, orderRepository, shoppingCartRepository, shippingAddressPageViewModel, billingAddressPageViewModel, paymentMethodPageViewModel, null, null); target.GoNextCommand.Execute(); Assert.IsTrue(target.IsShippingAddressInvalid); }