Skip to content

Commit e23e171

Browse files
authored
Merge pull request rails#47690 from andrewn617/add-on-load-callback-for-active-record-fixtures
Run a load hook when TestFixtures is included
2 parents 29441c0 + 49283ad commit e23e171

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
* Introduce `:active_record_fixtures` lazy load hook.
2+
3+
Hooks defined with this name will be run whenever `TestFixtures` is included
4+
in a class.
5+
6+
```ruby
7+
ActiveSupport.on_load(:active_record_fixtures) do
8+
self.fixture_paths << "test/fixtures"
9+
end
10+
11+
klass = Class.new
12+
klass.include(ActiveRecord::TestFixtures)
13+
14+
klass.fixture_paths # => ["test/fixtures"]
15+
```
16+
17+
*Andrew Novoselac*
18+
119
* Introduce `TestFixtures#fixture_paths`.
220

321
Multiple fixture paths can now be specified using the `#fixture_paths` accessor.

activerecord/lib/active_record/test_fixtures.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def after_teardown # :nodoc:
2525
class_attribute :pre_loaded_fixtures, default: false
2626
class_attribute :lock_threads, default: true
2727
class_attribute :fixture_sets, default: {}
28+
29+
ActiveSupport.run_load_hooks(:active_record_fixtures, self)
2830
end
2931

3032
module ClassMethods

activerecord/test/cases/test_fixtures_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def test_use_transactional_tests_can_be_overridden
2121
assert_equal "foobar", @klass.use_transactional_tests
2222
end
2323

24+
def test_inclusion_runs_active_record_fixtures_load_hook
25+
ActiveSupport.on_load(:active_record_fixtures) do
26+
self.fixture_paths << "test/fixtures"
27+
end
28+
klass = Class.new
29+
30+
klass.include(ActiveRecord::TestFixtures)
31+
32+
assert_includes klass.fixture_paths, "test/fixtures"
33+
end
34+
2435
unless in_memory_db?
2536
def test_doesnt_rely_on_active_support_test_case_specific_methods
2637
tmp_dir = Dir.mktmpdir

guides/source/engines.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ These are the load hooks you can use in your own code. To hook into the initiali
14931493
| `ActiveJob::Base` | `active_job` |
14941494
| `ActiveJob::TestCase` | `active_job_test_case` |
14951495
| `ActiveRecord::Base` | `active_record` |
1496+
| `ActiveRecord::TestFixtures` | `active_record_fixtures` |
14961497
| `ActiveStorage::Attachment` | `active_storage_attachment` |
14971498
| `ActiveStorage::VariantRecord` | `active_storage_variant_record` |
14981499
| `ActiveStorage::Blob` | `active_storage_blob` |

0 commit comments

Comments
 (0)