private PropertyRule<TestObject, object> CreateRule(Expression<Func<TestObject, object>> expression) { var builder = new RuleBuilder<TestObject, object>(expression); builder.SetValidator(new NotNullValidator()); return (PropertyRule<TestObject, object>)builder.Single(); }
public void Should_throw_when_property_name_is_null() { var builder = new RuleBuilder<Person, int>(x => x.CalculateSalary()); builder.GreaterThan(4); var ex = typeof(InvalidOperationException).ShouldBeThrownBy(() => builder.Single().Validate(new ValidationContext<Person>(new Person(), new PropertyChain(), new DefaultValidatorSelector())).ToList()); ex.Message.ShouldEqual("Property name could not be automatically determined for expression x => x.CalculateSalary(). Please specify either a custom property name by calling 'WithName'."); }
public void Nullable_object_with_condition_should_not_throw() { var builder = new RuleBuilder<Person, int>(x => x.NullableInt.Value); builder.GreaterThanOrEqualTo(3).When(x => x.NullableInt != null); builder.Single().Validate(new ValidationContext<Person>(new Person(), new PropertyChain(), new DefaultValidatorSelector())); }
public void Result_should_use_custom_property_name_when_no_property_name_can_be_determined() { var builder = new RuleBuilder<Person, int>(x => x.CalculateSalary()); builder.GreaterThan(100).WithName("Foo"); var results = builder.Single().Validate(new ValidationContext<Person>(new Person(), new PropertyChain(), new DefaultValidatorSelector())); results.Single().PropertyName.ShouldEqual("Foo"); }