Rails 5: Is there a way to run a single set of tests against the development database?

Multi tool use
Rails 5: Is there a way to run a single set of tests against the development database?
I would like to run a single set of tests against the development database. My seeds.rb
file populates the databse from a CSV and I want to ensure that the data is stored in the database in the way I expect. I don't want to run all tests against the development database, just a particular set.
seeds.rb
I created an integration test. I thought I could switch environments in #setup
but it looks like Rails.env = 'development'
has no effect.
#setup
Rails.env = 'development'
require 'test_helper'
class DbTest < ActionDispatch::IntegrationTest
def setup
Rails.env = 'development'
end
def test_total_settlements
...
Is it possible to run tests in different environments? If so, how is this done?
2 Answers
2
I'd recommend to create a class to seed the information into a configurable database, and then run tests against that class. In that way, you don't need to run the tests to an operational database and run that tests the number of times you want, without having to manually modify your development database in case the seed failed (like removing leftover records).
Once you have that class, you could add a task to your Rakefile and use your class :)
In my opinion, the simplest solution would be to just seed your test database.
You can call Rails.application.load_seed
before the tests you need seed data for.
Rails.application.load_seed
The problem with this option is I'm seeding the db with a lot of data and it would slow down the tests considerably. It takes about 5 minutes to seed the database.
– user3574603
Jul 1 at 12:01
One possible solution for this is to create a separate seed file for your test environment so that you only bring in the sample data that you need for your tests. The seedbank gem makes this pretty painless.
– Mark Merritt
Jul 1 at 20:54
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why not seeds the test database and check how it's populated ?
– Зелёный
Jul 1 at 5:34