我把Brian John的建議放在了他的所有觀點上,但我認爲我會從我的解決方案中發佈一些代碼,以防其他人想要做類似的事情。我還增加了一個自定義的元數據標籤的檢查,所以我只能這樣做時,我旗有測試組:文件符號
spec_helper.rb
注意,下面我支持我的#{Rails.root}public/system/ENV/files
目錄(其中ENV =「test」或「develop」),因爲我正在使用它來測試回形針功能,這就是我的文件存儲位置。
此外,我正在使用我的備份文件中的--delete
rysnc命令恢復目錄結構,而不僅僅是對目錄結構執行rm -r
,這將刪除在測試期間創建的所有文件。
RSpec.configure do |config|
# So we can tag tests with our own symbols, like we can do for ':js'
# to signal that we should backup and restore the filesystem before
config.treat_symbols_as_metadata_keys_with_true_values = true
config.before(:each) do
# If the example group has been tagged with the :file symbol then we'll backup
# the /public/system/ENV directory so we can roll it back after the test is over
if example.metadata[:file]
`rsync -a #{Rails.root}/public/system/#{Rails.env}/files/ #{Rails.root}/public/system/#{Rails.env}/files.back`
end
end
config.after(:each) do
# If the example group has been tagged with the file symbol then we'll revert
# the /public/system/ENV directory to the backup file we created before the test
if example.metadata[:file]
`rsync -a --delete #{Rails.root}/public/system/#{Rails.env}/files.back/ #{Rails.root}/public/system/#{Rails.env}/files/`
end
end
end
sample_spec。RB
請注意,我加了標籤的它「應該創建一個新的文件」與:文件符號
require 'spec_helper'
describe "Lesson Player", :js => true do
it "should create a new file", :file do
# Do something that creates a new file
...
end
end
+1閱讀我的心靈上的所有三個想法。這就是我經歷的確切思路。特別是github項目。我很驚訝它不在那裏。 – plainjimbo 2011-12-17 07:43:03