1

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

2 Answers 2

1

in your model you cant just say validate :all because :all isnt a column name.

class Suggestion < AR::Base
  validates_pressence_of :subject, :content
end

there would be no reason to validate the presence of id columns but i guess you can if you want.

api documentation: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jed, thanks for your answer. You are right. I don't know why I thought it would be possible to validate all model attributes in once through :all. This solves half of the problem: 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: It still gets error message on :type attribute
type is a reserved word. you'll have to change that in your fixtures and in your code. Try changing it to something like category, so it would read Suggestion.category
1

So at the end I found the answer for the problem related to :type attribute:

http://www.gyrotechie.com/2008/09/activerecord-does-not-like-attributes-called-type/

The problem was that type is a reserved field name for classes that inherit from ActiveRecord.

I renamed the field name through migration and modified all related files and all is running properly now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.