alex
2013-Sep-07 20:29 UTC
[shoulda-matchers gem] validate_numericality_of_matcher strange behaviour
Hello
I have Vacancy model and vacancy_spec test with validates_numericality_of
:salary, greater_than: 1 in the model and
validate_numericality_of(:salary).is_greater_than(2) in the _spec.
When i run rspec spec command tests pass (in some cases and in other they
don''t)
Why?
vacancy.rb
------------------------
class Vacancy < ActiveRecord::Base
validates_presence_of :name, :salary, :position, :description, :company,
:contact, :location
validates_length_of :name, :position, :company, :contact, :location, in:
3..50
validates_length_of :description, in: 3..250
validates_numericality_of :salary, greater_than: 1
end
vacancy_spec.rb
--------------------------
require ''spec_helper''
describe Vacancy do
[:name, :salary, :position, :description, :company, :contact,
:location].each do
|value| it { should validate_presence_of(value) }
end
[:name, :position, :company, :contact, :location].each do
|value| it { should
ensure_length_of(value).is_at_least(3).is_at_most(50) }
end
it { should ensure_length_of(:description).is_at_least(3).is_at_most(250)
}
it { should validate_numericality_of(:salary).is_greater_than(2) }
end
----------------------------------
Console output: (I''m running the same spec)
~/ror/test_app2$ rspec spec/models/vacancy_spec.rb
..............
Finished in 1.46 seconds
14 examples, 0 failures
Randomized with seed 36106
~/ror/test_app2$ rspec spec/models/vacancy_spec.rb
..............
Finished in 1.48 seconds
14 examples, 0 failures
Randomized with seed 898
~/ror/test_app2$ rspec spec/models/vacancy_spec.rb
.....F........
Failures:
1) Vacancy should only allow numeric, integer values for salary
Failure/Error: it { should
validate_numericality_of(:salary).is_greater_than(2) }
Expected errors when salary is set to 2, got errors: ["name
can''t
be blank (nil)", "name is too short (minimum is 3 characters)
(nil)",
"position can''t be blank (nil)", "position is too short
(minimum is 3
characters) (nil)", "description can''t be blank (nil)",
"description is too
short (minimum is 3 characters) (nil)", "company can''t be
blank (nil)",
"company is too short (minimum is 3 characters) (nil)", "contact
can''t be
blank (nil)", "contact is too short (minimum is 3 characters)
(nil)",
"location can''t be blank (nil)", "location is too short
(minimum is 3
characters) (nil)"]
# ./spec/models/vacancy_spec.rb:20:in `block (2 levels) in <top
(required)>''
Finished in 1.49 seconds
14 examples, 1 failure
Failed examples:
rspec ./spec/models/vacancy_spec.rb:20 # Vacancy should only allow numeric,
integer values for salary
Randomized with seed 60725
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/690b0eae-a569-4f12-bcab-e9af7b9cfd8a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Seemingly Similar Threads
- should validate_numericality_of with greater_than_or_equal_to
- validates_numericality_of and greater_than* doesn't appear to be working
- validates_numericality_of with greater_than* less_than* simply don't work
- At my wits end ! Controlling passed in negative values from a form
- RSpec, shoulda-matchers and Rails model attributes validations
