From 285d4c812c3100c75bb61a147f56b0ddfd5ba167 Mon Sep 17 00:00:00 2001 From: Justin Ko Date: Fri, 7 Jun 2024 20:36:14 -0600 Subject: [PATCH] Document TestFixtures#fixture Fixes #52000 Co-Authored-By: Hartley McGuire --- activerecord/lib/active_record/test_fixtures.rb | 13 ++++++++++--- activerecord/test/cases/fixtures_test.rb | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/test_fixtures.rb b/activerecord/lib/active_record/test_fixtures.rb index 00b3f5a8469..56f2fdf7b3e 100644 --- a/activerecord/lib/active_record/test_fixtures.rb +++ b/activerecord/lib/active_record/test_fixtures.rb @@ -96,6 +96,14 @@ module ActiveRecord end end + # Generic fixture accessor for fixture names that may conflict with other methods. + # + # assert_equal "Ruby on Rails", web_sites(:rubyonrails).name + # assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name + def fixture(fixture_set_name, *fixture_names) + active_record_fixture(fixture_set_name, *fixture_names) + end + private def run_in_transaction? use_transactional_tests && @@ -255,7 +263,7 @@ module ActiveRecord def method_missing(method, ...) if fixture_sets.key?(method.name) - _active_record_fixture(method, ...) + active_record_fixture(method, ...) else super end @@ -269,14 +277,13 @@ module ActiveRecord end end - def _active_record_fixture(fixture_set_name, *fixture_names) + def active_record_fixture(fixture_set_name, *fixture_names) if fs_name = fixture_sets[fixture_set_name.name] access_fixture(fs_name, *fixture_names) else raise StandardError, "No fixture set named '#{fixture_set_name.inspect}'" end end - alias_method :fixture, :_active_record_fixture def access_fixture(fs_name, *fixture_names) force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index c40d113ea77..57ef208c455 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -611,7 +611,7 @@ class FixturesTest < ActiveRecord::TestCase def test_fixture_method_and_private_alias assert_equal "The First Topic", topics(:first).title assert_equal "The First Topic", fixture(:topics, :first).title - assert_equal "The First Topic", _active_record_fixture(:topics, :first).title + assert_equal "The First Topic", active_record_fixture(:topics, :first).title end def test_fixture_method_does_not_clash_with_a_test_case_method