That defines the validation performed inside the Model class.
The issue comes afterwards when trying to test this on the Model's Spec (using Rspec for tests) file, which is done as follows:
MyModel_spec.rb
#latitude test for range validation using shoulda
it {should ensure_inclusion_of(:latitude).in_range(-90..90).with_low_message(/latitude must be greater than or equal to.*/).with_high_message(/latitude must be less than or equal to.*/)}
#longitude test for range validation using shoulda
it{ should ensure_inclusion_of(:longitude).in_range(-179..180).with_low_message(/longitude must be greater than.*/).with_high_message(/longitude must be less than.*/)}
it{ should ensure_exclusion_of(:longitude).in_range(-180..-180).with_message('longitude must be greater than -180 (-180.0)')}
#radius test for range validation using shoulda
it{should ensure_inclusion_of(:radius).in_range(10..100).with_low_message("radius must be greater than or equal to 10 (9)").with_high_message(/radius must be less than or equal to.*/)}
Now, from the rubyDoc of the shoulda gem:
_ensure_inclusion_of(attr)_
Ensure that the attribute’s value is in the range specified
Options:
in_range - the range of allowed values for this attribute
with_low_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :inclusion.
with_high_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :inclusion.
Example:
it { should ensure_inclusion_of(:age).in_range(0..100) }
From this, my understanding is that one should be able to call with_low_message(/longitude must be greater than.*/) with a Regex or a String of the expected error message. I've tried both ways and none of them seem to pass the Tests. the output of my console looks like:
Failures:
1) Place performs VALIDATIONS should ensure inclusion of radius in 10..1000
Failure/Error: it{should ensure_inclusion_of(:radius).in_range(10..100).with_low_message("radius must be greater than or equal to 10 (9)").with_highmessage(/radius must be less than or equal to./)}
_Expected errors to include "radius must be greater than or equal to 10 (9)"* when radius is set to 9, got errors: ["latitude can't be blank (nil)", "latitude is not a number (nil)", "longitude can't be blank (nil)", "longitude is not a number (nil)", "radius must be greater than or equal to 10 (9)"]
2) Place performs VALIDATIONS should ensure exclusion of longitude in -180..-180
Failure/Error: it{ should ensure_exclusion_of(:longitude).in_range(-180..-180).with_message('longitude must be greater than -180 (-180.0)')}
Expected errors to include "longitude must be greater than -180 (-180.0)" when longitude is set to -180, got errors: ["latitude can't be blank (nil)", "latitude is not a number (nil)", "radius can't be blank (nil)", "radius is not a number (nil)", "longitude must be greater than -180 (-180.0)"]
3) Place performs VALIDATIONS should ensure inclusion of latitude in -90..90
Failure/Error: it {should ensure_inclusion_of(:latitude).in_range(-90..90).with_lowmessage(/latitude must be greater than or equal to./).with_highmessage(/latitude must be less than or equal to./)}
*Expected errors to include /latitude must be greater than or equal to./ when latitude is set to -91, got errors: ["longitude can't be blank (nil)", "longitude is not a number (nil)", "radius can't be blank (nil)", "radius is not a number (nil)", "latitude must be greater than or equal to -90 (-91.0)"**]
I am developing under Ruby 1.9.3-p392, and rails 3.2.13 as well as the following gems (taken from gemFile):
gem 'rails', '3.2.13'
group :development, :test do
gem 'factory_girl_rails', '~> 4.2.1'
gem "rspec-rails", "~> 2.14.0"
end
group :test do
gem 'faker', '~> 1.1.2'
gem 'shoulda', '~> 3.5.0'
gem 'guard-rspec', '~> 3.0.2'
end
I've come upon a problem, when trying to test the validation of some model attributes, performed as: MyModel.rb
That defines the validation performed inside the Model class. The issue comes afterwards when trying to test this on the Model's Spec (using Rspec for tests) file, which is done as follows: MyModel_spec.rb
Now, from the rubyDoc of the shoulda gem:
_ensure_inclusion_of(attr)_ Ensure that the attribute’s value is in the range specified
Options:
Example:
it { should ensure_inclusion_of(:age).in_range(0..100) }
From this, my understanding is that one should be able to call with_low_message(/longitude must be greater than.*/) with a Regex or a String of the expected error message. I've tried both ways and none of them seem to pass the Tests. the output of my console looks like:
Failures:
1) Place performs VALIDATIONS should ensure inclusion of radius in 10..1000 Failure/Error: it{should ensure_inclusion_of(:radius).in_range(10..100).with_low_message("radius must be greater than or equal to 10 (9)").with_highmessage(/radius must be less than or equal to./)} _Expected errors to include "radius must be greater than or equal to 10 (9)"* when radius is set to 9, got errors: ["latitude can't be blank (nil)", "latitude is not a number (nil)", "longitude can't be blank (nil)", "longitude is not a number (nil)", "radius must be greater than or equal to 10 (9)"]
2) Place performs VALIDATIONS should ensure exclusion of longitude in -180..-180 Failure/Error: it{ should ensure_exclusion_of(:longitude).in_range(-180..-180).with_message('longitude must be greater than -180 (-180.0)')} Expected errors to include "longitude must be greater than -180 (-180.0)" when longitude is set to -180, got errors: ["latitude can't be blank (nil)", "latitude is not a number (nil)", "radius can't be blank (nil)", "radius is not a number (nil)", "longitude must be greater than -180 (-180.0)"]
3) Place performs VALIDATIONS should ensure inclusion of latitude in -90..90 Failure/Error: it {should ensure_inclusion_of(:latitude).in_range(-90..90).with_lowmessage(/latitude must be greater than or equal to./).with_highmessage(/latitude must be less than or equal to./)} *Expected errors to include /latitude must be greater than or equal to./ when latitude is set to -91, got errors: ["longitude can't be blank (nil)", "longitude is not a number (nil)", "radius can't be blank (nil)", "radius is not a number (nil)", "latitude must be greater than or equal to -90 (-91.0)"**]
I am developing under Ruby 1.9.3-p392, and rails 3.2.13 as well as the following gems (taken from gemFile):