Skip to content

Commit a19b13b

Browse files
authored
Merge pull request rails#48287 from shioyama/add_fixture_paths_to_engines
Automatically add `test/fixtures` in engines to `fixture_paths`
2 parents 0f5c8c5 + 908f1c9 commit a19b13b

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

activerecord/lib/active_record/fixtures.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class FixtureClassNotFound < ActiveRecord::ActiveRecordError # :nodoc:
2121
#
2222
# They are stored in YAML files, one file per model, which are placed in the directories
2323
# appointed by <tt>ActiveSupport::TestCase.fixture_paths=(path)</tt> (this is automatically
24-
# configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
24+
# configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>,
25+
# or in the <tt>test/fixtures</tt> folder under any of your application's engines).
2526
# The fixture file ends with the +.yml+ file extension, for example:
2627
# <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>).
2728
#

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add engine's `test/fixtures` path to `fixture_paths` in `on_load` hook if
2+
path exists and is under the Rails application root.
3+
4+
*Chris Salzberg*
5+
16
* `bin/rails app:template` now runs `bundle install` and any `after_bundle`
27
blocks after the template is executed.
38

railties/lib/rails/engine.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,15 @@ def load_seed
615615
end
616616
end
617617

618+
initializer :add_fixture_paths do
619+
next if is_a?(Rails::Application)
620+
621+
fixtures = config.root.join("test", "fixtures")
622+
if fixtures.exist? && fixtures.to_s.start_with?(Rails.root.to_s)
623+
ActiveSupport.on_load(:active_record_fixtures) { self.fixture_paths |= ["#{fixtures}/"] }
624+
end
625+
end
626+
618627
initializer :prepend_helpers_path do |app|
619628
if !isolated? || (app == self)
620629
app.config.helpers_paths.unshift(*paths["app/helpers"].existent)

railties/test/railties/engine_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ def index
304304
assert_equal "Hi bukkits\n", response[2].body
305305
end
306306

307+
test "adds its fixtures path to fixture_paths" do
308+
@plugin.write "test/fixtures/bukkits.yml", ""
309+
310+
boot_rails
311+
312+
test_class = Class.new
313+
test_class.singleton_class.attr_accessor :fixture_paths
314+
test_class.fixture_paths = []
315+
ActiveSupport.run_load_hooks(:active_record_fixtures, test_class)
316+
317+
assert_equal test_class.fixture_paths, ["#{Bukkits::Engine.root}/test/fixtures/"]
318+
end
319+
307320
test "adds its mailer previews to mailer preview paths" do
308321
@plugin.write "app/mailers/bukkit_mailer.rb", <<-RUBY
309322
class BukkitMailer < ActionMailer::Base

0 commit comments

Comments
 (0)