2012-03-18 112 views
4

我正在使用Ruby on Rails 3.2.2,cucumber-rails-1.3.0,rspec-rails-2.8.1和capybara-1.1.2。我想使用Selenium來測試JavaScript,但是每次我在終端窗口中運行cucumber命令行時,都不刪除test數據庫數據。也就是說,如果我聲明如下功能:如何在不刪除`test`數據庫數據的情況下測試JavaScript?

Feature: ... 

    @javascript 
    Scenario: ... 

JavaScript測試與預期的一樣。但是,在測試運行後,test數據庫數據將被刪除,並且我必須再次爲該數據庫播種以正確運行新測試。

我讀了Official Documentation和存在於ROOT_APP/features/support/env.rb文件中的文本(看來我安裝了所有必需的紅寶石寶石 - 請參閱下面的關於我使用的Gemfile詳細信息),但我不知道如何避免刪除數據庫數據以及如何配置Cucumber和Capybara寶石,以便正確使用Selenium。

我該做什麼?

注我:我想提出上述,因爲我想有相同test數據庫中的數據時,我的「測試」 /「運行」方案

注二:爲了種子在test數據庫中的數據(我的應用程序需要這些數據來工作),我想補充的RAILS_ROOT_PATH/lib/tasks/cucumber.rake文件下面的代碼,我運行從終端窗口中rake db:test:prepare命令行。

namespace :db do 
    namespace :test do 
    task :prepare => :environment do 
     Rake::Task["db:seed"].invoke 
    end 
    end 
end 

ROOT_APP/features/support/env.rb文件我試圖取消註釋下面的代碼塊的一個和兩個(BTW:我從來沒有改變由cucumber-rails寶石自動生成的原始文件,所以它是默認值爲1),但在運行測試後,它仍會刪除test數據庫數據。

# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do 
#  # { :except => [:widgets] } may not do what you expect here 
#  # as tCucumber::Rails::Database.javascript_strategy overrides 
#  # this setting. 
#  DatabaseCleaner.strategy = :truncation 
# end 
# 
# Before('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]') do 
#  DatabaseCleaner.strategy = :transaction 
# end 

的Gemfile摘錄:

group :development, :test do 
    gem "rspec-rails" 
end 

group :test do 
    gem 'cucumber-rails' 
    gem 'database_cleaner' 
    gem 'capybara' 
end 

回答

3

我碰到了同樣的問題,並設法通過改變ROOT_APP/features/support/env.rb

以下行從

Cucumber::Rails::Database.javascript_strategy = :truncation 
修復它

Cucumber::Rails::Database.javascript_strategy = :transaction 

希望這有助於...

相關問題