-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How to: Cleanup after your Rspec tests
chrisbloom7 edited this page Jun 7, 2011
·
8 revisions
If you are running tests that generate attachments, you can use an after(:all) callback in Rspec to do some cleaning up. The actual cleanup code will vary based on your setup, but here is an example:
RSpec.configure do |config|
# ...
config.after(:all) do
# Get rid of the linked images
if Rails.env.test? || Rails.env.cucumber?
tmp = Factory(:brand)
store_path = File.dirname(File.dirname(tmp.logo.url))
temp_path = tmp.logo.cache_dir
FileUtils.rm_rf(Dir["#{Rails.root}/public/#{store_path}/[^.]*"])
FileUtils.rm_rf(Dir["#{temp_path}/[^.]*"])
end
end
end
Please Note - the above code assumes that the store_path and cache_dir paths are separated based on the Rails environment. DO NOT just copy and past that into your spec_helper file unless you understand and have accounted for that. Otherwise you may end up deleting production files too. As an example, you can configure your paths per environment as such:
class MyUploader < CarrierWave::Uploader::Base
def cache_dir
"#{Rails.root}/tmp/uploads/#{Rails.env}/brands/logos"
end
def store_dir
"system/attachments/#{Rails.env}/brands/logos/#{model.friendly_id}/"
end
end