Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 996
nktokyo Avatar asked Jan 27 '11 14:01

nktokyo


2 Answers

Did you run rake db:test:prepare?

like image 81
psyho Avatar answered Nov 08 '22 15:11

psyho


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
like image 22
Dylan Markow Avatar answered Nov 08 '22 16:11

Dylan Markow