我在黄瓜和数据库事务方面遇到了一些问题 - 具体来说,当我运行测试套件时,之后没有清理数据库事务。
我正在使用 spork、postgres 运行 rails 3.1。
最初 features/support/env.rb 被设置为使用 database_cleaner,但在每次调用 db 时始终出现以下错误:
未指定数据库。缺少参数:数据库。(参数错误)/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/john /.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_pool.rb:302:incheckout_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
block (2 levels) 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:250:inloop' /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
block in checkout'/Users/john/。 rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:inmon_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
结帐'/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:inconnection' /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/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:inretrieve_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:inclean' /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/configuration.rb:56:inblock 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:inclean' /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 文件。任何建议都非常感谢 - 这个让我难倒了一段时间:
配置/数据库.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
配置/环境/黄瓜.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# 引发异常而不是渲染异常模板
config.action_dispatch.show_exceptions = false# 在测试环境中禁用请求伪造保护
config.action_controller.allow_forgery_protection = false# 告诉 Action Mailer 不要将电子邮件发送到现实世界。# :test 传递方法在 # ActionMailer::Base.deliveries 数组中累积发送的电子邮件。
config.action_mailer.delivery_method = :test# 创建测试数据库时使用 SQL 而不是 Active Record 的模式转储器。# 如果你的模式不能被模式转储器完全转储,这是必要的,# 比如你有约束或特定于数据库的列类型 # config.active_record.schema_format = :sql
# 将弃用通知打印到标准错误
config.active_support.deprecation = :stderr end
SPEC/SPEC_HELPER.RB
需要'rubygems' 需要'spork'
Spork.prefork do # 在此块中加载更多内容将使您的测试运行得更快。但是,# 如果您更改此处加载的库中的任何配置或代码,您将需要重新启动 spork 以使其生效。# 当你运行 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", FILE )时,这个文件被复制到 spec/ 'rspec/rails'
# 需要支持带有自定义匹配器和宏等的 ruby 文件,# 在 spec/support/ 及其子目录中。
Dir[Rails.root.join("spec/support/* / .rb")].each {|f| 要求 f}RSpec.configure 做 |config| # == Mock Framework # # 如果您更喜欢使用 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 固定装置 # config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run
事务中的每个 # 示例,删除以下行或指定 false # 而不是 true。config.use_transactional_fixtures = true 结束
结尾
Spork.each_run do # 每次运行规范时都会运行此代码。
结尾