spec/mvcli/validatable_spec.rb in mvcli-0.0.9 vs spec/mvcli/validatable_spec.rb in mvcli-0.0.10
- old
+ new
@@ -4,10 +4,12 @@
describe "a validator" do
use_natural_assertions
Given(:object) {Object.new}
Given(:validator) {MVCLI::Validatable::Validator.new}
Given(:validation) { validator.validate object }
+ Given(:violations) { validation.violations }
+
context "when it validates a field that does not exist on the object" do
Given {validator.validates(:does_not_exist, "invalid") {}}
When(:validation) {validator.validate object}
Then {not validation.errors[:does_not_exist].empty?}
Then {not validation.valid?}
@@ -33,8 +35,32 @@
And { not validation.violations[:property].empty? }
end
context "when the validate on nil option is not passed" do
When { validator.validates(:property, "check nil") {false}}
Then { validation.valid? }
+ end
+ end
+
+ describe "validating each element in an enumerable" do
+ Given { validator.validates(:foodles, "invalid", nil: true) {|foodle| not foodle.nil? } }
+ context "when there are invalid elements in the enumerable" do
+ When { object.stub(:foodles) {["not nil", nil, "not nil"]} }
+ Then { not validation.valid? }
+ Then { violations.has_key? "foodles[1]" }
+ And { not violations.has_key? "foodles"}
+ end
+ end
+
+ describe "validating an enumerable itself" do
+ Given { object.stub(:array) {array} }
+ Given { validator.validates(:array, "invalid", each: false) {|a| a.length < 3} }
+ context "when it is valid" do
+ When(:array) { [1,2] }
+ Then { validation.valid? }
+ end
+ context "when it is invalid" do
+ When(:array) { [1,2,3] }
+ Then { not validation.valid? }
+ And {not violations["array"].empty?}
end
end
end