【问题标题】:Rspec changing preference values, polluting development environmentRspec 改变偏好值,污染开发环境
【发布时间】:2012-03-13 01:00:43
【问题描述】:

更新:这根本不是关于我的数据库,而是关于 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

当我在本地主机(开发机器)上启动 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

数据库.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>

【问题讨论】:

  • 我应该补充一点,在我重新启动 Webrick 后,整个过程又开始了。 Webrick 找到丢失的数据库字段(大概仍在开发数据库中)。 Rspec 也会进行一次迭代。
  • 我只是使用了 Unicorn 而不是 WEbrick,它重现了同样的错误(尽管我必须运行两次 Rspec 才能出现)。
  • 在 Ruby 1.9.2 上也是一个问题。如果我注释掉ENV["RAILS_ENV"] ||= 'test',那么 Rspec 会继续通过,问题就会消失——但这不是正确的答案!如果我做到ENV["RAILS_ENV"] = 'test',问题仍然存在。

标签: ruby-on-rails testing rspec rspec-rails webrick


【解决方案1】:

事实证明,这是存储在缓存中的首选项的问题,而不是数据库的问题。

乔什·雅各布斯回答了这个问题(并且解释得很好)here

解决方案是为测试和开发配置不同的缓存存储。在 test.rb 中,我添加了 config.cache_store = :memory_store,重新启动了 spork,一切正常。

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多