Ruby on Rails
Saturday, September 7, 2013
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
-- 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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment