1

I have an application in rails 5.2.6 to which I am doing the test with RSPEC and Capybara and for it I have a test database with data that I use to perform the tests and I am running some tests that generate data that are stored in this database. What I need is that after running the tests these data are not stored in it.

I have used the gem database_cleaner but this cleans all the data of the test db and I only want to erase the data that are generated by the tests.

part of my database.yml:

development:
  <<: *default
  database: f_development
  host: localhost
  port: 5432
  username: postgres
  password: postgres

test:
  <<: *default
  database: f_test
  host: localhost
  port: 5432
  username: postgres
  password: postgres
  • Seems like you are using the same database for development and test. in your database.yml file change the `database: dev` and `database: test` different for both environment development and test. – Hiren Bhalani Jun 01 '21 at 15:48
  • Hi! I'm not, my database.yml has: development: <<: *default database: f_development host: localhost port: 5432 username: postgres password: postgres test: <<: *default database: f_test host: localhost port: 5432 username: postgres password: postgres – Andrea Elias Jun 01 '21 at 16:25
  • You should not rely on the existence of any data in test but rather generate all the data necessary during the testing process. This will eliminate your current concern and will keep your tests isolated and self sustaining. – engineersmnky Jun 01 '21 at 18:51

1 Answers1

0

Check your spec/rails_helper.rb configure section. Looks like you have set

config.use_transactional_fixtures = false

What means to not execute every test in transaction so data stays in the database.

Default behavior for this setting is true.

Yo can read how it work here

Another solution what can help you: you can setup seeding data before running test like described here

Alex Govorov
  • 26
  • 1
  • 4
  • if I setup seeding data it will create data before the tests, so there ll be no problem on having my database cleared, right? – Andrea Elias Jun 08 '21 at 17:08
  • @Andrea Elia Exactly. You configure your RSpec via rails_helper.rb to seed data before running test something like: `RSpec.configure do |config| config.before(:suite) do Rails.application.load_seed end end` – Alex Govorov Jun 08 '21 at 17:37