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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With