public void Should_not_validate_on_null_property_even_though_the_default_validator_for_it_was_registered() { // Arrange ValidatorFactory.Current.Register<OrderDetailValidator>(); var order = new Order(); var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(0, results.Count); }
public void GetValidationResult_returns_errors_if_item_is_null() { // Arrange var order = new Order(); var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(3, results.Count()); Assert.AreEqual("Order.OrderDetails", results[0].MemberName); Assert.AreEqual("OrderDetails must not be null.", results[0].Message); Assert.AreEqual("Order.OrderDetails", results[1].MemberName); Assert.AreEqual("OrderDetails cannot be empty.", results[1].Message); Assert.AreEqual("Order.OrderDetails", results[2].MemberName); Assert.AreEqual("Must have at least 2 order details.", results[2].Message); }
public void Should_register_default_validator_before_validation() { // Arrange ValidatorFactory.Current.Register<OrderDetailValidator>(); var order = new Order { FirstOrderDetail = new OrderDetail {ProductCode = "P1"}, }; var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(1, results.Count()); Assert.AreEqual("Order.FirstOrderDetail.ProductCode", results[0].MemberName); Assert.AreEqual("ProductCode must be between 3 and 5 in length. You had 2 in length.", results[0].Message); }
public void GetValidationResult_returns_errors_if_has_less_than_2_order_details() { // Arrange var order = new Order { OrderDetails = new [] { new OrderDetail() } }; var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(2, results.Count()); Assert.AreEqual("Order.OrderDetails", results[0].MemberName); Assert.AreEqual("Must have at least 2 order details.", results[0].Message); Assert.AreEqual("Order.OrderDetails", results[1].MemberName); Assert.AreEqual("First order detail must greater than $1000.", results[1].Message); }
public void GetValidationResult_returns_errors_if_second_order_detail_less_than_500() { // Arrange var order = new Order { OrderDetails = new[] { new OrderDetail { Amount = 100, Price = 10.01 }, new OrderDetail() } }; var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(1, results.Count()); Assert.AreEqual("Order.OrderDetails", results[0].MemberName); Assert.AreEqual("Second order detail must greater than $500.", results[0].Message); }
public void GetValidationResult_returns_no_errors_if_all_conditions_match() { // Arrange var order = new Order { OrderDetails = new[] { new OrderDetail { Amount = 10, Price = 100.01 }, new OrderDetail { Amount = 5, Price = 100.01 }, } }; var validator = new OrderValidator(); // Action var results = validator.GetValidationResult(order).ToList(); // Assert Assert.AreEqual(0, results.Count()); }
public void Throw_exception_if_use_a_validator_without_registration() { // Arrange var order = new Order { FirstOrderDetail = new OrderDetail(), }; var validator = new OrderValidator(); // Action validator.GetValidationResult(order).ToList(); }