rspec-rails: Test Database Schema never loads
Hey everyone, congrats on a great project. I’m having an issue and am at a loss for what to try next.
We’re using rspec-rails 3.0.0.beta2 with rails 4.1.8. Each time I run rspec (through bundle exec rspec, rake spec, or just rspec), it comes back with an ActiveRecord query error that a table from my schema doesn’t exist in my test database:
/Users/miles/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in
query': Mysql2::Error: Table 'equity_eats_test.projects' doesn't exist: SELECTprojects.* FROMprojectsWHEREprojects.deleted_at` IS NULL (ActiveRecord::StatementInvalid)
I saw the upgrade guide about loading the test db, so I verified that we have the following in our spec_helper.rb:
ActiveRecord::Migration.maintain_test_schema! if defined?(ActiveRecord::Migration)
but the schema is never loaded:
mysql> use equity_eats_test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
so then I try to manually update the test database by doing one of: RAILS_ENV=test rake db:migrate or RAILS_ENV=test rake db:reset, but I get the same ActiveRecord query error from above – no such table.
I’ve also tried upgrading rspec-rails to the latest version with no avail.
What should I try next?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 25 (10 by maintainers)
Do any tables exist or is it just that one? I would suggest connecting to the test DB directly and see what is going on. If you manually ran
RAILS_ENV=test rake db:resetthat should have fixed it. This doesn’t sound like an RSpec issue, but a Rails issue with your test ENV.Note, I also found that in our setup the gem was only in the
testgroup. By not being in both the test and development group - the require ofrspec-railswhich is essentially the Rails::Railtie file and hence has therake_taskscall to loadrspec/rails/tasks/rspec.rakemeant that the hooks for test:prepare were never called. Something else to look out for. Cheers.Rails advocates for
db:test:prepareto be run if you need to force the test schema to be synchronized. From their changelog entry:The
ActiveRecord::Migration.maintain_test_schema!is supposed to do this for us. In my case, I found that it was not working due to theload_schema_if_pending!because it assumes project bin stubs. See below:So I found that simply adding bin stubs to the Rails project hooked ever everything up just nicely. Note, only the first line is needed, the second one to add an rspec bin stub is optional butrecommended.
I know this is deprecated, but this fixed this for me: