From 0d8260d1359add1a4a53faac2395dc19859289d8 Mon Sep 17 00:00:00 2001 From: Ponomarev Nikolay Date: Sun, 19 Oct 2014 21:09:07 +0400 Subject: [PATCH] a little of refactoring CarrierWave::Test::MockFile --- spec/spec_helper.rb | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2fe62adb9..6f82b0bb6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -59,46 +59,29 @@ def mock_storage(kind) end module MockFiles - def stub_merb_tempfile(filename) - raise "#{path} file does not exist" unless File.exist?(file_path(filename)) - - t = Tempfile.new(filename) - FileUtils.copy_file(file_path(filename), t.path) - - return t - end - def stub_tempfile(filename, mime_type=nil, fake_name=nil) raise "#{path} file does not exist" unless File.exist?(file_path(filename)) - t = Tempfile.new(filename) - FileUtils.copy_file(file_path(filename), t.path) - - # This is stupid, but for some reason rspec won't play nice... - eval <<-EOF - def t.original_filename; '#{fake_name || filename}'; end - def t.content_type; '#{mime_type}'; end - def t.local_path; path; end - EOF - - return t + tempfile = Tempfile.new(filename) + FileUtils.copy_file(file_path(filename), tempfile.path) + tempfile.stub(:original_filename => fake_name || filename, + :content_type => mime_type) + tempfile end + alias_method :stub_merb_tempfile, :stub_tempfile + def stub_stringio(filename, mime_type=nil, fake_name=nil) - if filename - t = StringIO.new( IO.read( file_path( filename ) ) ) - else - t = StringIO.new - end - t.stub(:local_path => "", - :original_filename => filename || fake_name, - :content_type => mime_type) - return t + file = IO.read( file_path( filename ) ) if filename + stringio = StringIO.new(file) + stringio.stub(:local_path => "", + :original_filename => filename || fake_name, + :content_type => mime_type) + stringio end def stub_file(filename, mime_type=nil, fake_name=nil) - f = File.open(file_path(filename)) - return f + File.open(file_path(filename)) end end