Integration test is not running in development env

2019-08-17 06:33发布

问题:

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 %>

回答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.



回答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)