Skip to content

Commit f45efb2

Browse files
author
Laurent Cobos
committed
[FIX-1554] fix fixture_file_upload path resolution
1 parent c543765 commit f45efb2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

lib/rspec/rails/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def self.initialize_configuration(config)
8181
config.add_setting :file_fixture_path, :default => 'spec/fixtures/files'
8282
config.include RSpec::Rails::FileFixtureSupport
8383
end
84+
85+
# Add support for fixture_path on fixture_file_upload
86+
config.include RSpec::Rails::FixtureFileUploadSupport
87+
8488
# This allows us to expose `render_views` as a config option even though it
8589
# breaks the convention of other options by using `render_views` as a
8690
# command (i.e. `render_views = true`), where it would normally be used
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module RSpec
2+
module Rails
3+
# @private
4+
module FixtureFileUploadSupport
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
delegate :fixture_file_upload, :to => :rails_fixture_file_wrapper
9+
end
10+
11+
private
12+
13+
def rails_fixture_file_wrapper
14+
resolved_fixture_path = (fixture_path || RSpec.configuration.fixture_path || '')
15+
RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '')
16+
RailsFixtureFileWrapper.instance
17+
end
18+
19+
class RailsFixtureFileWrapper
20+
class << self
21+
attr_reader :fixture_path
22+
23+
# Get the instance of wrapper
24+
def instance
25+
@instance ||= new
26+
end
27+
28+
# Override fixture_path set
29+
# to support Rails 3.0..3.1 using ActionController::TestCase class to resolve fixture_path
30+
# see https://apidock.com/rails/v3.0.0/ActionDispatch/TestProcess/fixture_file_upload
31+
def fixture_path=(value)
32+
if ActionController::TestCase.respond_to?(:fixture_path)
33+
ActionController::TestCase.fixture_path = value
34+
end
35+
@fixture_path = value
36+
end
37+
38+
include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
39+
end
40+
end
41+
end
42+
end
43+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
module RSpec::Rails
4+
describe FixtureFileUploadSupport do
5+
context 'with fixture path set in config' do
6+
before { RSpec.configuration.fixture_path = File.dirname(__FILE__) }
7+
8+
it 'resolves fixture file' do
9+
expect(fixture_file_upload_resolved.run).to be true
10+
end
11+
end
12+
13+
context 'with fixture path set in spec' do
14+
it 'resolves fixture file' do
15+
expect(fixture_file_upload_resolved(File.dirname(__FILE__)).run).to be true
16+
end
17+
end
18+
19+
def fixture_file_upload_resolved(fixture_path = nil)
20+
RSpec::Core::ExampleGroup.describe do
21+
include RSpec::Rails::FixtureFileUploadSupport
22+
23+
self.fixture_path = fixture_path if fixture_path
24+
25+
it 'supports fixture file upload' do
26+
file = fixture_file_upload(File.basename(__FILE__))
27+
expect(file.read).to match(/describe FixtureFileUploadSupport/im)
28+
end
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)