nktokyo
nktokyo

Reputation: 642

rspec, unknown attribute question

I am working through the (excellent) railstutorial.org site have have a basic question about rspec.

When I run the below test on a new user model, I get an "unknown attribute: username" message and a failed test.

  before(:each) do
   @attr = { :lname_e => "User", :fname_e => "Test", :email => "[email protected]", :username => "testUser" }
  end

  it "should create a new instance given valid attributes" do
    User.create!(@attr)
  end

Error syntax is

Failures:
  1) User should create a new instance given valid attributes
     Failure/Error: User.create!(@attr)
     unknown attribute: username
     # ./spec/models/user_spec.rb:11:in `block (2 levels) in <top (required)>'

The field is in the users table (string), it's in the model as attr_accessible and in the console I can create a user with exactly the same syntax in the test. This "username" field was added via a migration after creating the initial table, is there some other file I need to update here?

Thanks,

Upvotes: 14

Views: 3615

Answers (2)

psyho
psyho

Reputation: 7212

Did you run rake db:test:prepare?

Upvotes: 31

Dylan Markow
Dylan Markow

Reputation: 124419

The field may be missing from your test database, but present in your development database (which is why the console is working).

Try making sure your migrations are all up to date and then update the test database:

rake db:migrate
rake db:test:prepare

Upvotes: 22

Related Questions