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