微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ruby on Rails Rspec 使用了错误的环境和数据库

如何解决Ruby on Rails Rspec 使用了错误的环境和数据库

这个让我有点困惑。

全新的 Rails 6.1.3、Ruby 2.7.0 应用程序,带有 Rspec 和 database_cleaner

设置应用程序和 Rspec 后,我刚刚为 User 模型创建了一个快速单元测试,然后创建了一个快速请求测试来检查认根页面

我遇到的第一个问题是我的请求测试失败。该请求没有显示仅包含一个带有 Home 的 h1 标记的根页面,而是返回一条关于 www.example.com 是被阻止的主机的错误消息,并将 example.com as 添加到 config.hosts

这是我第一次遇到这种情况,不久前我使用 Rails 6.1 和 Ruby 2.6.4 创建了另一个应用程序,配置基本相同,但从未出现过主机受阻问题。

所以我在我的 test.rb 环境中将 example.com 添加到 config.hosts。同样的问题。我去把它添加到 development.rb 环境文件中,它通过了。这应该是我的第一个危险信号。

在开发模式下玩弄我的应用程序并测试用户注册添加删除用户之后。我再次运行我的测试套件,然后发现我在开发中使用的所有数据都消失了。所以我意识到 Rspec 实际上是在开发环境中运行并在我的开发数据库上运行。即使我的 rails_helper.rb 文件确实有 ENV['RAILS_ENV'] ||='test' 行。

我可以让它正常运行的唯一方法是像这样明确地运行我的 rspec:RAILS_ENV="test" bundle exec rspec spec/requests/pages_spec.rb

但我从来没有这样做过。甚至在我最近创建的应用程序中也没有。那么这是 Rails 6.1.3 中的新东西吗?或者也许是更新的版本或 Rspec?

有人遇到过这种情况吗?让我感到困惑的是,两个 Rails 应用程序的创建时间可能相隔几个月,除了 Ruby 版本和 Rails 版本不同之外,其他 gem 几乎相同。

这里是特定文件的详细信息

宝石文件

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.0'
gem 'rails','~> 6.1.3'
gem 'pg','~> 1.1'
gem 'puma','~> 5.0'
gem 'sass-rails','>= 6'
gem 'webpacker','~> 5.0'
gem 'turbolinks','~> 5'
gem 'jbuilder','~> 2.7'
gem 'bootsnap','>= 1.4.4',require: false
gem 'devise','~> 4.7','>= 4.7.3'
gem 'interactor','~> 3.0'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
gem 'faker'

group :development,:test do
  gem 'byebug',platforms: [:mri,:mingw,:x64_mingw]
  gem 'factory_bot_rails','6.1.0'
  gem 'rspec-rails','4.0.2'
  gem 'spring-commands-rspec'
end

group :development do
  gem 'web-console','>= 4.1.0'
  gem 'rack-mini-profiler','~> 2.0'
  gem 'listen','~> 3.3'
  gem 'spring'
end

gem 'tzinfo-data',platforms: [:mingw,:mswin,:x64_mingw,:jruby]

group :test do 
  gem 'capybara','3.35.0'
  gem 'selenium-webdriver','3.142.6'
  gem 'database_cleaner'
  gem 'geckodriver-helper'
  gem 'webdrivers','4.1.2'
end

rails_helper.rb

require 'spec_helper'
require 'factory_bot_rails'
require 'devise'
require 'database_cleaner'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment',__dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'

Dir[Rails.root.join('spec','support','**','*.rb')].sort.each { |f| require f }



begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.include FactoryBot::Syntax::Methods
  config.include ControllerMacros,type: :request
  config.include Features::SessionHelpers,type: :feature
  config.include Devise::Test::IntegrationHelpers,type: :request
  config.include Devise::Test::IntegrationHelpers,type: :view
  config.include Devise::Test::IntegrationHelpers,type: :feature
  config.include Warden::Test::Helpers

  config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do 
      example.run 
    end
  end

  config.after(:each) do 
    Warden.test_reset!
  end

  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!

  Capybara.javascript_driver = :selenium_chrome 

  FactoryBot::SyntaxRunner.class_eval do
    include Actiondispatch::TestProcess
    include ActiveSupport::Testing::FileFixtures
  end
end

数据库.yml

# Postgresql. Versions 9.3 and up are supported.

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
  <<: *default
  database: trunfo_development
  host: localhost
  pool: 5
  username: <%= ENV.fetch("DB_USER") %> 
  password: <%= ENV.fetch("DB_PASSWORD") %>

test:
  <<: *default
  database: trunfo_test
  host: localhost
  pool: 5
  username: <%= ENV.fetch("DB_USER") %> 
  password: <%= ENV.fetch("DB_PASSWORD") %>

解决方法

也许某个地方的 RAILS_ENV 已经被设置为“测试”以外的其他东西。 您是否尝试打印出 env 以查看? 在您的帮助文件中写入 ENV['RAILS_ENV'] = 'test' 会有所不同吗?

,

尝试在 ENV['RAILS_ENV'] ||= 'test' 中设置 project_name/test/test_helper.rb

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。