gpt4 book ai didi

ruby-on-rails - Rspec 正在使用我的开发数据库

转载 作者:行者123 更新时间:2023-12-04 02:33:16 26 4
gpt4 key购买 nike

我正在使用 Rspec 进行测试。但是,它使用我的开发数据库而不是我的测试数据库。这怎么会发生?

我只是使用 rspec 来运行我的测试:don:my_project_root $ rspec
其他问题的常见错误似乎只与 Rails 3 相关,或者已经过时了(使用不再与 rspec 一起使用的命令)。

下面是我的 spec_helper.rb。

ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require "capybara/rspec"
require 'database_cleaner'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false # commented-out during tim's tutorial

# Rails cast tutorial
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"

config.include FactoryGirl::Syntax::Methods

config.include Capybara::DSL
end

数据库.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000

最佳答案

我不知道你们中是否还有人遇到这个问题,但对于我来说

ENV["RAILS_ENV"] ||= 'test'

来自 rails_helper.rbspec_helper.rb 的顶部修复。 spec_helper.rb加载前做一些事情 rails_helper.rb ,并且在那段时间里可能有一些东西触及了数据库。

关于ruby-on-rails - Rspec 正在使用我的开发数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com