UPDATE:这根本不是关于我的数据库,而是关于Spree在我的测试污染的内存缓存中存储的首选项。请参阅下面的答案。
我在Rails 3.1.4上运行Spree应用程序,使用Ruby 1.9.3-p0,rspec-rails 2.8.1,rspec 2.8.0。
Rspec在我的开发机器上与Webrick奇怪地交互,让我觉得它以某种方式使一个正在运行的Webrick实例切换到使用测试数据库。
我的HomeController有一个规范:
require 'spec_helper'
describe HomeController do
render_views
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
end
当我在我的localhost(开发机器)上启动Webrick时,我的主页在浏览器中正常工作。我第一次在我的HomeController上运行Rspec,其索引操作对应于主页,我的测试通过了。
在Rspec运行之后,Webrick中的页面中断,因为之前填充了文本数据的数据库字段现在为零。随后的Rspec迭代也会中断。
那么,Rspec让Webrick停止使用我的开发数据库并开始使用我的测试数据库,为什么?它是spec_helper.rb中的ENV["RAILS_ENV"] ||= 'test'
行吗?
(这是我的spec_helper.rb和已清理的database.yml:)
spec_helper.rb(我安装了Spork和Autotest,但是当我没有运行它时会发生此错误。)
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# require 'capybara/rspec'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
#require 'factories'
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
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.include Devise::TestHelpers, :type => :controller
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
@configuration ||= AppConfiguration.find_or_create_by_name("Default configuration")
PAYMENT_STATES = Payment.state_machine.states.keys unless defined? PAYMENT_STATES
SHIPMENT_STATES = Shipment.state_machine.states.keys unless defined? SHIPMENT_STATES
ORDER_STATES = Order.state_machine.states.keys unless defined? ORDER_STATES
# Usage:
#
# context "factory" do
# it { should have_valid_factory(:address) }
# end
RSpec::Matchers.define :have_valid_factory do |factory_name|
match do |model|
Factory(factory_name).new_record?.should be_false
end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
的database.yml
development:
adapter: mysql2
host: localhost
database: <APP>_development
username: <USER>
password: <PASSWORD>
# 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: mysql2
host: localhost
database: <APP>_test
username: <USER>
password: <PASSWORD>
production:
adapter: mysql2
host: localhost
database: <APP>_prod
username: <PROD_USER>
password: <PROD_PASSWORD>
答案 0 :(得分:0)
事实证明,这是存储在缓存中的首选项的问题,而不是数据库。
Josh Jacobs回答(并解释得非常好)here
解决方案是为测试和开发配置不同的缓存存储。在test.rb中,我添加了config.cache_store =:memory_store,重启了spork,一切正常。