导轨:黄瓜不清洁DB

我在黄瓜和数据库事务方面遇到了一些问题 – 具体来说,当我运行测试套件时,之后不会清理数据库事务。

我正在使用spork,postgres运行rails 3.1。

最初feature / support / env.rb设置为使用database_cleaner,但在每次调用db时始终出现以下错误:

没有指定数据库。 缺少参数:数据库。 (ArgumentError)/Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:22:in postgresql_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in new_connection’/Users/ postgresql_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in .rvm/gems /ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:302:in checkout_new_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:254:in在checkout checkout_new_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:254:in块(2个级别)’ checkout_new_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:254:in 1.9 .2-p290 / gems / activerecord-3.1.0.rc4 / lib / active_record / connection_adapters / abstract / connection_pool.rb:250:in loop' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:250:in /Users/john/.rvm/gems/ruby-1.9.2-p290/ loop' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:250:in in checkout’ loop' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:250:in p29 0 / lib / ruby​​ / 1.9.1 / monitor.rb:201:在mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in / mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in / mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in / mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in /ruby- mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in / mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in / mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in /lib/ mon_synchronize' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:249:in checkout’/Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/ connection_pool.rb:151: connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:388:in retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in Users retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in john retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in .rvm retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in gems retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in ruby-1.9.2-p290 retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in gems retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in activerecord-3.1.0.rc4 retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in lib retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in active_record retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in connection_adapters retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in abstract retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in connection_specification.rb connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:388:in 107 connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:388:in retrieve_connection' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/active_record.rb:7:in connection’/ Users / john /。 rvm / gems / ruby​​-1.9.2-p290 / gems / database_cleaner-0.6.7 / lib / database_cleaner / active_record / transaction.rb:17:in clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/base.rb:77:in /Users/john/.rvm/gems/ruby-1.9.2 clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/base.rb:77:in clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/base.rb:77:in clean’ clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/base.rb:77:in block in clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/configuration.rb:56:in each’/Users/john/.rvm /gems/ruby-1.9.2-p290/gems/database_cleaner-0.6.7/lib/database_cleaner/configuration.rb:56:in clean' /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.0.2/lib/cucumber/rails/hooks/database_cleaner.rb:9:in After’

我试过恢复常规交易,但没有快乐。 我可以通过控制台(黄瓜环境)访问数据库并创建/删除/检索记录没有问题。 下面添加了config / database.yml,features / support / env.rb,config / environments / cucumber.rb和rspec / spec_helper.rb文件。 任何建议都非常感激 – 这个让我难过了一段时间:

CONFIG / database.yml的

 default: &defaults adapter: postgresql username: rails_dev password: foobar development: <<: *defaults database: fcct_d test: &test <<: *defaults database: fcct_t production: <<: *defaults database: fcct_p username: fcct password: k#1*5Avb3dTa cucumber: adapter: postgresql username: rails_dev password: foobar database: fcct_t 

特点/技术支持/ ENV.RB

 require 'rubygems' require 'spork' Spork.prefork do require 'cucumber/rails' require 'database_cleaner' require 'database_cleaner/cucumber' Capybara.default_selector = :css ActionController::Base.allow_rescue = false begin DatabaseCleaner.strategy = :transaction rescue NameError raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." end end Spork.each_run do require 'factory_girl' require File.join(File.dirname(__FILE__), '../../lib/existing_factory/existing_factory.rb') end 

CONFIG /环境中/ CUCUMBER.RB

BusinessschoolCorporate :: Application.configure do#此处指定的设置优先于config / application.rb中的设置

#测试环境专门用于运行您的应用程序

测试套件。 否则你永远不需要使用它。 记得

#您的测试数据库是测试套件的“临时空间”并被擦除#并在测试运行之间重新创建。 不要依赖那里的数据!
config.cache_classes = true
config.use_transactional_fixtures = true

#为具有Cache-Control的测试配置静态资产服务器,以获得性能config.serve_static_assets = true
config.static_cache_control =“public,max-age = 3600”

#在nil上意外调用方法时记录错误消息
config.whiny_nils = true

#显示完整的错误报告并禁用缓存
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

#引发exception而不是呈现exception模板
config.action_dispatch.show_exceptions = false

#在测试环境中禁用请求伪造保护
config.action_controller.allow_forgery_protection = false

#告诉Action Mailer不要向现实世界发送电子邮件。 #:test delivery方法在#ActionMailer :: Base.deliveries数组中累积已发送的电子邮件。
config.action_mailer.delivery_method =:test

#在创建测试数据库时,使用SQL而不是Active Record的模式转储器。 #如果架构转储器无法完全转储您的架构,则必须这样做#,如果您有约束或特定于数据库的列类型#config.active_record.schema_format =:sql

#打印弃用通知给stderr
config.active_support.deprecation =:stderr end

SPEC / SPEC_HELPER.RB

要求’rubygems’需要’spork’

Spork.prefork做#在此块中加载更多将导致您的测试运行得更快。 但是,#如果您从此处加载的库中更改任何配置或代码,您将需要重新启动spork才能生效。 #此文件被复制到spec /当你运行’rails generate rspec:install’ENV [“RAILS_ENV”] || =’test’需要File.expand_path(“../../ config / environment”, FILE )require “RSpec的/ Rails的

#需要使用自定义匹配器和宏等支持ruby文件,#spec / support /及其子目录。
Dir [Rails.root.join(“spec / support / * / .rb”)]。每个{| f | 要求f}

RSpec.configure do | config | #==模拟框架##如果您更喜欢使用mocha,flexmock或RR,请取消注释相应的行:## config.mock_with:mocha#config.mock_with:flexmock#config.mock_with:rr config.mock_with:rspec

 # 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 

事务中的每个#example,删除以下行或指定false#而不是true。 config.use_transactional_fixtures = true end

结束

Spork.each_run do#每次运行规范时都会运行此代码。

结束

对于那些想知道的人 – 事实上我在config / database.yml文件中有默认值。 数据库清理程序有一个自己的“默认”调用,它拦截配置信息。

为什么不尝试在CONFIG/ENVIRONMENTS/CUCUMBER.RB文件中输入config.use_transactional_fixtures = true