【发布时间】:2021-03-16 18:00:01
【问题描述】:
这个让我有点困惑。
带有 Rspec 和 database_cleaner 的全新 Rails 6.1.3、Ruby 2.7.0 应用程序
在设置应用程序和 Rspec 之后,我刚刚为 User 模型创建了一个快速单元测试,然后是一个快速请求测试来检查默认根页面。
我遇到的第一个问题是我的请求测试失败。该请求没有显示根页面,其中仅包含一个带有 Home 的 h1 标记,而是返回一条关于 www.example.com 被阻止的主机并将 example.com 添加到 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") %>
【问题讨论】:
标签: database rspec ruby-on-rails-6