Support file_fixture in Factory definitions#427
Conversation
dorianmariecom
left a comment
There was a problem hiding this comment.
I would test an actual file upload if possible. Otherwise looks good to me
|
This is our first time extending I can't think of how this could break existing tests but maybe I'm just not being creative enough. |
8a739aa to
5a1a15e
Compare
0cd66df to
33c1348
Compare
33c1348 to
61baf5f
Compare
611f31f to
f878964
Compare
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
f878964 to
43e18b9
Compare
|
This should be added to the NEWS as well (under a |
Follow-up to [#427][] Add an entry to the `NEWS.md` file to document support for [file_fixture][] added in [#427][]. [#427]: #427 [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
Follow-up to [#427][] Add an entry to the `NEWS.md` file to document support for [file_fixture][] added in [#427][]. [#427]: #427 [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
Follow-up to [#427][] Add an entry to the `NEWS.md` file to document support for [file_fixture][] added in [#427][]. [#427]: #427 [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
Follow-up to [#427][] Add an entry to the `NEWS.md` file to document support for [file_fixture][] added in [#427][]. [#427]: #427 [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
|
Hi @seanpdoyle - here's an anecdote for you in case it's useful elsewhere. Having the option to opt out of this turned out to be necessary (and worked). My tests already included fixture support, which I had mixed into FactoryBot in a different way here. After updating to 6.5.0, my Rspec tests continued to pass but my integration tests (written in Spinach) all started to fail with the same error: I think this is because the Railtie assumes the testing system is Rspec, and if it's something other than Rspec, it leaves |
|
I ran into a problem with this because I use FactoryBot for This was my workaround: # config/initializers/factory_bot.rb
return unless defined?(FactoryBot)
require 'active_support/testing/file_fixtures'
module RailsFileFixturesAnywhere
extend ActiveSupport::Testing::FileFixtures
def self.fixture_file_upload(filename)
Rack::Test::UploadedFile.new file_fixture(filename).to_s
end
def self.file_fixture_path
Rails.root.join("spec/fixtures/files")
end
end
FactoryBot::SyntaxRunner.include FactoryBotRails::FileFixtureSupport
FactoryBot::SyntaxRunner.file_fixture_support = RailsFileFixturesAnywhere |
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::TestCaseprovide a file_fixture helper to construct aPathnameinstance based on the path defined byActiveSupport::TestCase.file_fixture_path(relative to the Rails root directory).