Through rspec (I'm using rspec-1.3.0, rspec-rails-1.3.2 gems) generator (ruby script/generate rspec_model suggestion section_id:integer user_id:integer subject:string content:text state:string type:string
) I created model and model spec and run rake db:migrate and rake:test:prepare
After that I started to work on my model spec:
require 'spec_helper'
describe Suggestion do
before(:each) do
@valid_attributes = {
:section_id => 1,
:user_id => 1,
:subject => 'Inappropriate title',
:content => 'The title of this section is inappropriate.',
:state => 'new',
:type => 'flag'
}
end
it "should create a new instance given valid attributes" do
Suggestion.create!(@valid_attributes)
end
it "should reject empty section_id attribute" do
empty_section_id_suggestion = Suggestion.new(@valid_attributes.merge(:section_id => ""))
empty_section_id_suggestion.should_not be_valid
end
...
Apart from 1st "should create a new instance given valid attributes"
test I created 6 tests, basically each testing attribute of suggestion model for being empty - almost exactly same as "should reject empty section_id attribute"
example.
When I run tests I get 6 failed tests, which is fine. First test "should create a new instance given valid attributes"
passes.
Now when I go the the suggestion model and add validates_presence_of :all
I get following error message related to 1st test:
ActiveRecord::RecordInvalid in 'Suggestion should create a new instance given valid attributes'
Validation failed: All can't be blank
./spec/models/suggestion_spec.rb:16:
When I try to run tests in isolation (validates_presence_of :attribute
) all tests are passing, only with :type
attribute I get again similar error message:
ActiveRecord::RecordInvalid in 'Suggestion should create a new instance given valid attributes'
Validation failed: Type can't be blank
./spec/models/suggestion_spec.rb:16:
I haven't encountered this problem before (have multiple similar models and their specs passing properly). It looks like it has problem with the :type attribute (it says it can't be empty), even I'm passing value to it through @valid_attributes
. I tried to Google search but didn't find similar problem/solution.
Here is the test for :type attribute
it "should reject empty type attribute" do
empty_type_suggestion = Suggestion.new(@valid_attributes.merge(:type => ""))
empty_type_suggestion.should_not be_valid
end
Please check it out and let me know what I'm doing wrong here.
Thanks a lot for help
Peter