10

I have this in my group_spec.rb file:

describe Group do
  it { should have_many(:users) }
end

and this in my user_spec.rb file:

describe User do
  it { should belong_to(:group) }
end

When I run the tests, I get:

Failure/Error: it { should have_many(:users) }
ActiveRecord::StatementInvalid:
PGError: ERROR:  relation "users" does not exist

   LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                           ^
   :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                 FROM pg_attribute a LEFT JOIN pg_attrdef d
                   ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                WHERE a.attrelid = '"users"'::regclass
                  AND a.attnum > 0 AND NOT a.attisdropped
                ORDER BY a.attnum

In my group.rb file I have:

has_many :users

And in my users.rb file I have:

belongs_to :group

I feel like I'm missing something that should be obvious. Any help would be appreciated. Thanks!

John

zishe
  • 10,665
  • 12
  • 64
  • 103
John
  • 13,125
  • 14
  • 52
  • 73
  • 18
    Try running `rake db:migrate:reset db:test:prepare` to make sure the test database is up-to-date. If it doesn't work, please post your migrations and models. – cuvius Nov 28 '11 at 21:32
  • That did the trick! I had tried rake db:drop db:create db:migrate. . . why did that not work? Does that not cover the test database? If you make your comment an answer I can accept it. – John Nov 28 '11 at 21:47
  • 3
    Indeed, it doesn't. Unless you set `RAILS_ENV=test` or use a Rake task that specifically targets the test DB (as `db:test:prepare` does), the migrations will run against the development DB. – Marnen Laibow-Koser Nov 28 '11 at 23:05
  • Please see possible answer [here][1]. [1]: http://stackoverflow.com/a/19804714/446267 – Малъ Скрылевъ Nov 06 '13 at 05:27
  • 1
    @MarnenLaibow-Koser Thanks for your note here. I was hitting this (irritating) issue. A simple run of `RAILS_ENV=test rake db:migrate` was a solve for me. For another route, using the rake command instead - `rake spec ....` will properly prepare the test db before execution. – justinraczak Feb 06 '15 at 02:20

2 Answers2

29

Had this same problem and used the solution in the comment from cuvius. Posting here so that people don't miss it!

Run: RAKE_ENV=test rake db:migrate:reset db:test:prepare to set up your test database.

you786
  • 3,659
  • 5
  • 48
  • 74
apb
  • 3,270
  • 3
  • 29
  • 23
  • 2
    If you're getting this error because you removed a table, make sure you've removed your fixtures .yml file. – Schneems Jul 18 '12 at 22:51
1

Unfortunately rake db:test:prepare is deprecated in rails 4+ so it's not best solution now.
I guess that in your user factory class written as User. The problem occurs because factories loads before migrations done.
So to solve this:
Change in your factory class name from

factory :user, class: User do
  # ...
end

to

factory :user, class: 'User' do
  # ...
end