2011-03-29 65 views
2

我在哪裏可以編寫代碼來全球所有燈具的加載後只能被執行之後運行一次代碼,運行所有測試前/規格扶手:加載所有全球燈具

我嘗試過(:套件)使用RSpec 1.3.1在Rails 2.3.11上,似乎在裝置之前執行。

回答

0

rake任務(/ lib/tasks)如何?舉例來說,我有一個(reset_db.rake)加載夾具,重置db和更多:

namespace :db do 
    desc "Drop, create, migrate, seed the database and prepare the test database for rspec" 
    task :reset_db => :environment do 
    Rake::Task['db:drop'].invoke 
    Rake::Task['db:create'].invoke 
    Rake::Task['db:migrate'].invoke 
    Rake::Task['db:fixtures:load'].invoke 
    Rake::Task['db:test:prepare'].invoke 
    end 
end 
0

我遇到同樣的問題,但仍然沒有發現任何方式掛鉤裝燈具後一些代碼...所以我使用了SpyrosP解決方案。然而,這種做法的問題在於,您不能再從Fixtures訪問器助手中受益,因爲您不會從rspec配置中加載固定設備,而是從rake任務中加載固定設備。

所以基本上你neeed重現這樣的幫手theese(代碼是有點髒,但似乎爲我工作:

#spec_helper.rb 

    module CustomAccessors 
    # Remplacement de fixtures :all 
    %w{yml csv}.each do |format| 
     paths = Dir. 
     glob(::Rails.root.join("spec/fixtures/*.#{format}")). 
     map! { |path| path.match(/\/([^\.\/]*)\./)[1] } 

     paths.each do |path| 
     define_method path do |*args| 
      path.singularize.camelcase.constantize.find(ActiveRecord::Fixtures.identify(args[0])) 
     end 
     end 
    end 
    end 

    RSpec.configure do |config| 
    #config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.include(CustomAccessors) 
    end