public void ShouldDeliverErrorsWhenModelObservableValidationTriggers() { var viewModel = new IndeiTestViewModel(); const string namesShouldMatchMessage = "names should match."; viewModel.ValidationRule( vm => vm.OtherName, viewModel.WhenAnyValue( m => m.Name, m => m.OtherName, (name, other) => name == other), namesShouldMatchMessage); Assert.False(viewModel.HasErrors); Assert.True(viewModel.ValidationContext.IsValid); Assert.Single(viewModel.ValidationContext.Validations); Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>()); Assert.Empty(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>()); viewModel.Name = "JoJo"; viewModel.OtherName = "NoNo"; Assert.True(viewModel.HasErrors); Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>()); Assert.Single(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>()); Assert.Single(viewModel.ValidationContext.Text); Assert.Equal(namesShouldMatchMessage, viewModel.ValidationContext.Text.Single()); }
public void ValidationRulesOfTheSamePropertyShouldNotDuplicate() { var viewModel = new IndeiTestViewModel(); viewModel.ValidationRule( m => m.Name, m => m is not null, "Name shouldn't be null."); viewModel.ValidationRule( m => m.Name, m => !string.IsNullOrWhiteSpace(m), "Name shouldn't be white space."); Assert.False(viewModel.ValidationContext.IsValid); Assert.Equal(2, viewModel.ValidationContext.Validations.Count); }
public void ShouldSendPropertyChangeNotificationsForCorrectProperties() { var viewModel = new IndeiTestViewModel(); viewModel.ValidationRule( m => m.Name, m => m is not null, "Name shouldn't be null."); viewModel.ValidationRule( m => m.OtherName, m => m is not null, "Other name shouldn't be null."); Assert.Single(viewModel.GetErrors(nameof(viewModel.Name))); Assert.Single(viewModel.GetErrors(nameof(viewModel.OtherName))); var arguments = new List <DataErrorsChangedEventArgs>(); viewModel.ErrorsChanged += (_, args) => arguments.Add(args); viewModel.Name = "Josuke"; viewModel.OtherName = "Jotaro"; Assert.Equal(2, arguments.Count); Assert.Equal(nameof(viewModel.Name), arguments[0].PropertyName); Assert.Equal(nameof(viewModel.OtherName), arguments[1].PropertyName); Assert.False(viewModel.HasErrors); viewModel.Name = null; viewModel.OtherName = null; Assert.Equal(4, arguments.Count); Assert.Equal(nameof(viewModel.Name), arguments[2].PropertyName); Assert.Equal(nameof(viewModel.OtherName), arguments[3].PropertyName); Assert.True(viewModel.HasErrors); }