Raphael
Raphael

Reputation: 1721

rspec failing - "Couldn't find table", but rake db:test:prepare doesn't fix it

I've been doing a major refactor that required several migrations. All of a sudden, rspec is failing.

Could not find table 'users' (ActiveRecord::StatementInvalid)

The users table is right there in schema.rb:

  create_table "users", :primary_key => "user_id", :force => true do |t|
    t.string   "first_name",          :limit => 100,                                    :null => false
    t.string   "last_name",           :limit => 100,                                    :null => false
    (...)
    t.boolean  "current_student",                    :default => true,                  :null => false
    t.boolean  "unregistered",                       :default => false,                 :null => false
  end

Here's what I've tried:

My app is working fine - I can create users, log in, log out, etcetera. But none of my tests work. What should I try next?

Upvotes: 2

Views: 2297

Answers (2)

Raphael
Raphael

Reputation: 1721

Found the issue.

In one of my models, I had a scope written thus:

scope :registered, where(:registered => true)

Deep in the stack trace, there was actually a reference to this line. I changed the scope to:

scope :registered, :conditions => {:registered => true}

...and everything worked. Whew.

Upvotes: 0

iwasrobbed
iwasrobbed

Reputation: 46703

Run rake db:test:load instead to create the test database from the schema.rb file

The rake db:test:load recreates the test database from the current db/schema.rb. On subsequent attempts, it is a good idea to first run db:test:prepare, as it first checks for pending migrations and warns you appropriately.

More info: http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing

Upvotes: 4

Related Questions