This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Validatable 1.6.6 released
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
You can replace custom validation assertions with the newly introduced validate_only instance method. The validate_only method is my latest attempt to make it easy to test model validations one at a time.
The following code should provide a decent example.
classSampleModel includeValidatable validates_presence_of:name,:address attr_accessor:name,:address end
unit_tests do test "individual validation for name is executed"do instance =SampleModel.new instance.validate_only("presence_of/name") assert_equal "can't be empty", instance.errors.on(:name) end
test "individual validation for address is not executed"do instance =SampleModel.new instance.validate_only("presence_of/name") assert_equal nil, instance.errors.on(:address) end end
The validate_only method accepts an argument that is a DSL for running a single validation. The format of the DSL is "[validation type]/[key or attribute]". If the validation has a key set you should provide the key, otherwise the attribute will suffice. The documentation provides the following examples.
validates_presence_of :name can be run with obj.validate_only("presence_of/name")
validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")