Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop rspec from dropping the test database before tests

I have two Rails apps that use the same database. One app is managing the database through migrations but the other is just accessing it.

For some reason when I run tests with RSpec in the app that is not managing the database it drops the database before running the tests. But because this app does not know how to recreate the database all tests will fail.

How can I tell RSpec not to drop the database, just use it as it is?

like image 261
Mika Avatar asked Oct 25 '16 11:10

Mika


People also ask

Does RSpec clean database?

I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.

Can you use RSpec without rails?

# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # file to always be loaded, without a need to explicitly require it in any files. # the additional setup, and require it from the spec files that actually need it.

How do I run an RSpec test locally?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.


2 Answers

If you don't need to migrate the database you can redefine rspecs-rails spec:prepare task like this:

lib/tasks/patch_rspec_rails.rb

Rake::Task["spec:prepare"].clear
namespace :spec do
  task :prepare do
    ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
  end 
end

The original spec:prepare task callstest:prepare, which setups the db.

The task test:prepare exists since Rails 4.0 (or maybe earlier). This task also exists within Rails 5.0. It is a hook for railsties to add test dependent setups. You can check its definition with rake -W test:prepare. That the task is hit you can check with rake --trace spec.

ActiveRecord uses this task to check the migration state and setup the db.

When this task is not called, no db will be dropped or created.

But be aware, when some other gem uses test:prepare as a hook too plug into tests, it will not work.

Edit:

Since Rails 4.1 you can set config.active_record.maintain_test_schema = false within config/environments/test.rb. This way Rails should no longer try to migrate your test schema.

like image 85
slowjack2k Avatar answered Oct 01 '22 21:10

slowjack2k


Ideally RSpec should be reinitialisation the database for testing to ensure your environment is in a reliably, predictable state.

What you could do is for the Rails app that isn't managing the database carry out a rake db:schema:dump to generate the schema.rb which will then be used by RSpec - of course make sure that your database.yml test configuration isn't pointing to your live database.

I know that this isn't technically a solution to your question but it should prevent the underlying issue which is causing your tests to fail.

like image 38
David Avatar answered Oct 01 '22 22:10

David