PetrolMan
PetrolMan

Reputation: 381

Rspec Create/Update Unknown Attribute Error

I'm using the latest versions of Rails, Rspec and Factory Girl and I'm getting a strange issue when I try to test my create or update logic. The controller in question is an Admin namespaced PostsController and the model is Post. The factory itself just creates a post with a title and a body.

describe 'create' do
before :all do
    @new = Factory.build(:post)
end

it 'should be successful' do
  post :create, :post => @new
  response.should be_success
end

describe 'failure' do
  it 'should not create a new page' do
    lambda do
      post :create, :post => @new
    end.should_not change(Post, :count)
  end

  it 'should render the new template' do
    post :create, :post => @new
    response.should render_template('new')
  end
end

end

The error I keep receiving is:

ActiveRecord::UnknownAttributeError: unknown attribute: post

I'm probably doing something extremely stupid but I'm just lost right now.

UPDATE

Just in case anyone should ever stumble across this...

I was doing something extremely stupid. I had an error in my controller where instead of calling Post.new(params[:post]) I was calling Post.new(params)...

Upvotes: 1

Views: 752

Answers (1)

Steve Jorgensen
Steve Jorgensen

Reputation: 12371

It would help to know what line it's failing on. If it's failing in the 'before :all' block, then the problem is probably in your factory code, which is presumably specifying a value for a non-existent 'post' attribute of the model.

If that's what the factory is doing, but the 'post' attribute actually should exist, then perhaps you ran this using rspec from the command line without running rake db:test:prepare first. In that case, your 'posts' table structure might not be up to date.

Upvotes: 2

Related Questions