Value does not exist when running rspec tests

I have this in my group_spec.rb file:

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

and this is 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 that I am missing something that should be obvious. Any help would be greatly appreciated. Thanks!

John

+6
source share
2 answers

Had the same problem and used the solution in a comment from cuvius. Held here so people don’t miss it!

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

+23
source

Unfortunately rake db: test: it is not recommended to prepare in rails 4 + , so this is not the best solution now. I assume that in your user factory the class is written as User . The problem arises because factories are loading before migrations are completed.
So, to solve this problem:
Changing factory class name from

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

to

 factory :user, class: 'User' do # ... end 
+1
source

Source: https://habr.com/ru/post/902592/


All Articles