-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
file_fixture
in Factory definitions
Related to [factory_bot#1282][] [rails/rails#45606][] has been merged and is likely to be released as part of Rails 7.1. With that addition, the path toward resolving [factory_bot#1282][] becomes more clear. If factories can pass along [Pathname][] instances to attachment attributes, Active Support will handle the rest. Instances of `ActiveSupport::TestCase` provide a [file_fixture][] helper to construct a `Pathname` instance based on the path defined by `ActiveSupport::TestCase.file_fixture_path` (relative to the Rails root directory). [factory_bot#1282]: thoughtbot/factory_bot#1282 (comment) [rails/rails#45606]: rails/rails#45606 [Pathname]: https://docs.ruby-lang.org/en/master/Pathname.html [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
- Loading branch information
1 parent
346e3c7
commit 43e18b9
Showing
16 changed files
with
193 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module FactoryBotRails | ||
module FileFixtureSupport | ||
def self.included(klass) | ||
klass.cattr_accessor :file_fixture_support | ||
|
||
klass.delegate :file_fixture, to: "self.class.file_fixture_support" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "factory extensions" do | ||
describe "#file_fixture" do | ||
it "delegates to the test harness" do | ||
FactoryBot.define do | ||
factory :upload, class: Struct.new(:filename) do | ||
filename { file_fixture("file.txt") } | ||
end | ||
end | ||
|
||
upload = FactoryBot.build(:upload) | ||
|
||
expect(Pathname(upload.filename)).to eq(file_fixture("file.txt")) | ||
end | ||
|
||
it "uploads an ActiveStorage::Blob" do | ||
FactoryBot.define do | ||
factory :active_storage_blob, class: ActiveStorage::Blob do | ||
filename { pathname.basename } | ||
|
||
transient do | ||
pathname { file_fixture("file.txt") } | ||
end | ||
|
||
after :build do |model, factory| | ||
model.upload factory.pathname.open | ||
end | ||
end | ||
end | ||
|
||
blob = FactoryBot.create(:active_storage_blob) | ||
|
||
expect(blob.filename.to_s).to eq("file.txt") | ||
expect(blob.download).to eq(file_fixture("file.txt").read) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
ActiveStorage::Engine.root.glob("db/migrate/*.rb").each { |file| require file } | ||
|
||
ActiveSupport.on_load :active_support_test_case do | ||
setup do | ||
CreateActiveStorageTables.migrate :up | ||
end | ||
|
||
teardown do | ||
CreateActiveStorageTables.migrate :down | ||
rescue ActiveRecord::StatementInvalid | ||
# no-op | ||
end | ||
end | ||
|
||
defined?(RSpec) && RSpec.configure do |config| | ||
config.before do | ||
CreateActiveStorageTables.migrate :up | ||
end | ||
|
||
config.after do | ||
CreateActiveStorageTables.migrate :down | ||
rescue ActiveRecord::StatementInvalid | ||
# no-op | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class FactoryBotRails::FactoryTest < ActiveSupport::TestCase | ||
self.file_fixture_path = "test/fixtures/files" | ||
|
||
test "delegates #file_fixture to the test harness" do | ||
FactoryBot.define do | ||
factory :upload, class: Struct.new(:filename) do | ||
filename { file_fixture("file.txt") } | ||
end | ||
end | ||
|
||
upload = FactoryBot.build(:upload) | ||
|
||
assert_equal file_fixture("file.txt"), upload.filename | ||
end | ||
|
||
test "uploads an ActiveStorage::Blob" do | ||
FactoryBot.define do | ||
factory :active_storage_blob, class: ActiveStorage::Blob do | ||
filename { pathname.basename } | ||
|
||
transient do | ||
pathname { file_fixture("file.txt") } | ||
end | ||
|
||
after :build do |model, factory| | ||
model.upload factory.pathname.open | ||
end | ||
end | ||
end | ||
|
||
blob = FactoryBot.create(:active_storage_blob) | ||
|
||
assert_equal "file.txt", blob.filename.to_s | ||
assert_equal file_fixture("file.txt").read, blob.download | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
# Configure Rails Environment | ||
ENV["RAILS_ENV"] = "test" | ||
|
||
require_relative "../spec/fake_app" | ||
|
||
require "rails/test_help" | ||
require "factory_bot_rails" | ||
|
||
Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) } |