0

I am new in ruby on rails and I want to write a simple test to check the login functionality of my system. I am following this official documentation http://guides.rubyonrails.org/testing.html#integration-testing

I run the command rails test:integration but this is showing this error

rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `development` environment.
You are running in `test` environment. If you are sure you want to continue, first set the environment using:

bin/rails db:environment:set RAILS_ENV=test

Why I should I switch to test environment to test my application? If i switch to test environment all the configurations will be different. Is this strange that we develop in development/production mode and then test in testing mode?

This is my testing db

default: &default
  adapter:  postgresql
  encoding: unicode
  host:     <%= ENV.fetch("DATABASE_HOST") { "localhost" } %>
  username: <%= ENV.fetch("DATABASE_USER") {"vagrant"} %>
  password: <%= ENV.fetch("DATABASE_PASS") {"vagrant"} %>
  pool:     <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: fanshub_development
  socket:   <%= ENV["DATABASE_SOCKET"] %>

test:
  <<: *default
  database: fanshub_test
  socket:   <%= ENV["DATABASE_SOCKET"] %>

production:
  <<: *default
  database: fanshub_production
  <% if ENV["DATABASE_URL"] %>
  host:     <%= ENV.fetch("DATABASE_URL", "localhost") %>
  <% elsif ENV["DATABASE_SOCKET"] %>
  socket:   <%= ENV["DATABASE_SOCKET"] %>
  <% end %>
john
  • 611
  • 1
  • 7
  • 33

2 Answers2

2

Rails test env recreates a clean state of the database for every test run. If you run tests on dev environment your dev data will be erased

Assure you run tests in test env with a separate test database (configured in database.yml)

mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • So should I run the command bin/rails db:environment:set RAILS_ENV=test? – john Mar 10 '18 at 15:12
  • Test commands are run automatically in test env. You need to have a test db confiured in `config/databases.yml`. see https://stackoverflow.com/a/7304629/580346 – mrzasa Mar 10 '18 at 15:20
  • 1
    Try `rake db:test:prepare` and then run a test – mrzasa Mar 10 '18 at 15:44
1

If i switch to test environment all the configurations will be different. Is this strange that we develop in development/production mode and then test in testing mode?

No, it’s very common to test in a dedicated test environment. This makes tests predictable. You can initialize the test environment with fixtures.

Aaron Breckenridge
  • 1,723
  • 18
  • 25