集成测试未在开发环境中运行 [英] Integration test is not running in development environment

查看:252
本文介绍了集成测试未在开发环境中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails中的红宝石新手,我想编写一个简单的测试来检查系统的登录功能.我正在遵循此官方文档 http://guides.rubyonrails.org/testing.html#集成测试

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

我运行rails test:integration命令,但这显示了此错误

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?

这是我的测试数据库

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

推荐答案

Rails test env为每次测试运行重新创建数据库的干净状态.如果您在开发环境中运行测试,则开发数据将被删除

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

确保您使用单独的测试数据库(在database.yml中配置)在测试环境中运行测试

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

这篇关于集成测试未在开发环境中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆