1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
require 'spec_helper'
describe CarrierWave::Uploader do
let(:uploader_class) { Class.new(CarrierWave::Uploader::Base) }
let(:uploader) { uploader_class.new }
let(:file_name) { 'test.jpg' }
let(:file) { File.open(file_path(file_name)) }
after { FileUtils.rm_rf(public_path) }
describe '#remove!' do
let(:stored_file) { double('a stored file') }
before do
allow(CarrierWave).to receive(:generate_cache_id).and_return('1390890634-26112-1234-2122')
cached_file = double('a cached file')
allow(cached_file).to receive(:delete)
allow(stored_file).to receive(:path).and_return('/path/to/somewhere')
allow(stored_file).to receive(:url).and_return('http://www.example.com')
allow(stored_file).to receive(:identifier).and_return('this-is-me')
allow(stored_file).to receive(:delete)
storage = double('a storage engine')
allow(storage).to receive(:store!).and_return(stored_file)
allow(storage).to receive(:cache!).and_return(cached_file)
allow(storage).to receive(:delete_dir!).with("uploads/tmp/#{CarrierWave.generate_cache_id}")
allow(uploader_class.storage).to receive(:new).and_return(storage)
uploader.store!(file)
end
it "resets the current path" do
uploader.remove!
expect(uploader.current_path).to be_nil
end
it "should not be cached" do
uploader.remove!
expect(uploader).not_to be_cached
end
it "resets the url" do
uploader.cache!(file)
uploader.remove!
expect(uploader.url).to be_nil
end
it "resets the identifier" do
uploader.remove!
expect(uploader.identifier).to be_nil
end
it "deletes the file" do
expect(stored_file).to receive(:delete)
uploader.remove!
end
it "resets the cache_name" do
uploader.cache!(file)
uploader.remove!
expect(uploader.cache_name).to be_nil
end
it "does nothing when trying to remove an empty file" do
expect{ uploader.remove! }.not_to raise_error
end
end
end
|