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)

Most upvoted comments

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:reset that 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 test group. By not being in both the test and development group - the require of rspec-rails which is essentially the Rails::Railtie file and hence has the rake_tasks call to load rspec/rails/tasks/rspec.rake meant that the hooks for test:prepare were never called. Something else to look out for. Cheers.

Rails advocates for db:test:prepare to be run if you need to force the test schema to be synchronized. From their changelog entry:

ActiveRecord::Base.maintain_test_schema now uses db:test:prepare to synchronize the schema. Plugins can use this task as a hook to provide custom behavior after the schema has been loaded. NOTE: test:prepare runs before the schema is synchronized. Fixes #17171, #15787. Yves Senn

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 the load_schema_if_pending! because it assumes project bin stubs. See below:

system("bin/rake db:test:prepare")

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.

$ bundle exec rake rails:update:bin
$ bundle binstub rspec-rails

I know this is deprecated, but this fixed this for me:

$ rake db:test:prepare