/
spec_helper.rb
99 lines (78 loc) · 2.42 KB
/
spec_helper.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# encoding: utf-8
$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'rubygems'
if ENV["AS"]
puts "--> using ActiveSupport"
require 'activesupport'
elsif ENV["EXTLIB"]
puts "--> using Extlib"
require 'extlib'
end
require 'tempfile'
#require 'ruby-debug'
require 'spec'
require 'spec/autorun'
require 'carrierwave'
require 'logger'
CarrierWave.logger = Logger.new(nil)
alias :running :lambda
def file_path( *paths )
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
end
def public_path( *paths )
File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
end
CarrierWave.config[:public] = public_path
CarrierWave.config[:root] = File.expand_path(File.dirname(__FILE__))
module CarrierWave
module Test
module MockStorage
def mock_storage(kind)
storage = mock("storage for #{kind} uploader")
storage.stub!(:setup!)
storage
end
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
end
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).and_return("")
t.stub!(:original_filename).and_return(filename || fake_name)
t.stub!(:content_type).and_return(mime_type)
return t
end
def stub_file(filename, mime_type=nil, fake_name=nil)
f = File.open(file_path(filename))
return f
end
end
end
end
Spec::Runner.configure do |config|
config.include CarrierWave::Test::Matchers
config.include CarrierWave::Test::MockFiles
config.include CarrierWave::Test::MockStorage
end