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: SELECT
projects.* FROM
projectsWHERE
projects.
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?
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.
None of the tables exist in the test database. That's what my mysql commands show.
config/environments/test.rb
is the default test environment file that rails produces. Is there something that needs to be set in there? I didn't see anything in the rspec-rails README.
Try RAILS_ENV=test rake db:schema:load
, also check you are not overriding the ENV['RAILS_ENV']
variable anywhere
RAILS_ENV=test rake db:schema:load
gives the same error. Also, the only place we're setting the rails env is in the spec_helper if it's not set yet:
./spec/spec_helper.rb:ENV["RAILS_ENV"] ||= 'test'
No I mean anywhere in your application/ Rakefile etc, as it sounds like your Rakefile is ending up in development mode or something, as it's not migrating your test DB, but rspec is definitely not using the test db.
± |staging ✓| → grep -Ri rails_env .
./log/development.log: bin/rake db:migrate RAILS_ENV=development
./spec/spec_helper.rb:ENV["RAILS_ENV"] ||= 'test'
and RACK_ENV which has the same effect
nothing:
± |staging ✓| → grep -Ri rack_env .
Hmph,
Can you gist your config/environments/test.rb
and config/database.yml
(with any relevant sensitive information scrubbed of course)
If I read your comment correctly you're say running this command also generates the error when it is run (i.e. the command itself produces error output)?
RAILS_ENV=test rake db:schema:load
That's correct @cupakromer. It's almost like something about the test environment forces rails/rspec to run some queries before it migrates the test database.
Ah! You're migrations are failing to run, possibly because you have ActiveRecord commands in them?
Are you using spring? Try changing reconnect: false
to true
in your config/database.yml
you'll probably have to restart spring...
We are using spring, but changing reconnect: false
to reconnect: true
and stopping spring produced the same error.
Also, the first line of the spec_helper
is ENV["RAILS_ENV"] ||= 'test'
-- is that correct? Or should it be ENV["RAILS_ENV"] = 'test'
I don't think you should be focusing on RSpec. I would focus on your failing rake db task. Since that has the same error then this is not caused by RSpec. I would instead focus on your Rakefile and/or Rails config.
Thanks @cupakromer and @JonRowe for your help. I'll look more into why the rake db task is failing and come back if it's definitely an issue with rspec.
Just a follow up: I had forgotten we whitelist vanity urls in config/routes and it was trying to query our projects table for the vanity urls when the projects table wasn't created yet.
I know this is deprecated, but this fixed this for me:
$ rake db:test:prepare
I was also facing the same issue and rake db:test:prepare
fixed it for me
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 usesdb: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.
```shell
$ bundle exec rake rails:update:bin
$ bundle binstub rspec-rails
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.
run: rake db:migrate RAILS_ENV=test
and then: rspec spec