From 41929908ed47cfd56b7592504f3181bbfa3460a2 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 12 May 2023 09:22:51 +0100 Subject: [PATCH] Merge pull request #2671 from jaywhy/main Fixes issue with ActiveStorage::FixtureSet.blob causing a TypeError --- features/file_fixture.feature | 19 +++++++++++++++++++ features/support/rails_versions.rb | 15 +++++++++++++++ lib/rspec/rails/file_fixture_support.rb | 3 +++ 3 files changed, 37 insertions(+) create mode 100644 features/support/rails_versions.rb diff --git a/features/file_fixture.feature b/features/file_fixture.feature index b87fb4746e..c6a765a530 100644 --- a/features/file_fixture.feature +++ b/features/file_fixture.feature @@ -36,3 +36,22 @@ Feature: Using `file_fixture` """ When I run `rspec spec/lib/file_spec.rb` Then the examples should all pass + + @rails_post_7 + Scenario: Creating a ActiveStorage::Blob from a file fixture + Given a file named "spec/fixtures/files/sample.txt" with: + """ + Hello + """ + And a file named "spec/lib/fixture_set_blob.rb" with: + """ruby + require "rails_helper" + + RSpec.describe "blob" do + it "creates a blob from a sample file" do + expect(ActiveStorage::FixtureSet.blob(filename: "sample.txt")).to include("sample.txt") + end + end + """ + When I run `rspec spec/lib/fixture_set_blob.rb` + Then the examples should all pass diff --git a/features/support/rails_versions.rb b/features/support/rails_versions.rb new file mode 100644 index 0000000000..95354a0b35 --- /dev/null +++ b/features/support/rails_versions.rb @@ -0,0 +1,15 @@ +def rails_version + string_version = ENV.fetch("RAILS_VERSION", "~> 7.0.0") + if string_version == "main" || string_version.nil? + Float::INFINITY + else + string_version[/\d[.-]\d/].tr('-', '.') + end +end + +Before "@rails_post_7" do |scenario| + if rails_version.to_f < 7.0 + warn "Skipping scenario #{scenario.name} on Rails v#{rails_version}" + skip_this_scenario + end +end diff --git a/lib/rspec/rails/file_fixture_support.rb b/lib/rspec/rails/file_fixture_support.rb index 45059a38d1..85745c6e39 100644 --- a/lib/rspec/rails/file_fixture_support.rb +++ b/lib/rspec/rails/file_fixture_support.rb @@ -9,6 +9,9 @@ module FileFixtureSupport included do self.file_fixture_path = RSpec.configuration.file_fixture_path + if defined?(ActiveStorage::FixtureSet) + ActiveStorage::FixtureSet.file_fixture_path = RSpec.configuration.file_fixture_path + end end end end