Search code examples
ruby-on-railsrspecrspec-rails

Tell Rspec - don't delete my test_db when before running tests


I populate a database that I want to run rspec tests against.

Rspec seems to delete the db before it starts the tests. How can I tell Rspec not to delete this test_db?

I populate the db outside of Rails / Rspec environment. To build all the factories needed to build up the DB is just not practical for the scope of the project but I would like to test against existing DB.


Solution

  • You can manage clean your database using database cleaner gem, and The documentation recommends the following configuration for rspec as this link, but you change as the following:

    RSpec.configure do |config|
    
      config.before(:suite) do
        DatabaseCleaner.strategy = :transaction
        DatabaseCleaner.clean_with(:truncation)
      end
    end
    

    But, I prefer to create your database in test environment before scenario, as example:

    describe "specific name for this scenario" do
      before do
        @object = create(:object) # using factory girl as example
      end
    end
    

    so, this data'll be generate for each time you run your test-case, read more about creating data for rspec-test.