Skip to content

Commit 9229dcf

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

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

lib/rspec/rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require 'rspec/rails/matchers'
1111
require 'rspec/rails/fixture_support'
1212
require 'rspec/rails/file_fixture_support'
13+
require 'rspec/rails/fixture_file_upload_support'
1314
require 'rspec/rails/example'
1415
require 'rspec/rails/vendor/capybara'
1516
require 'rspec/rails/configuration'

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module RSpec
2+
module Rails
3+
# @private
4+
module FixtureFileUploadSupport
5+
delegate :fixture_file_upload, :to => :rails_fixture_file_wrapper
6+
7+
private
8+
9+
def rails_fixture_file_wrapper
10+
resolved_fixture_path = (fixture_path || RSpec.configuration.fixture_path || '')
11+
RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '')
12+
RailsFixtureFileWrapper.instance
13+
end
14+
15+
class RailsFixtureFileWrapper
16+
include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
17+
18+
class << self
19+
attr_reader :fixture_path
20+
21+
# Get the instance of wrapper
22+
def instance
23+
@instance ||= new
24+
end
25+
26+
# Override fixture_path set
27+
# to support Rails 3.0->3.1 using ActionController::TestCase class to resolve fixture_path
28+
# see https://apidock.com/rails/v3.0.0/ActionDispatch/TestProcess/fixture_file_upload
29+
def fixture_path=(value)
30+
if ActionController::TestCase.respond_to?(:fixture_path)
31+
ActionController::TestCase.fixture_path = value
32+
end
33+
@fixture_path = value
34+
end
35+
end
36+
end
37+
end
38+
end
39+
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)