Merge pull request #52054 from justinko/issue-52000

Document TestFixtures#fixture
This commit is contained in:
Rafael Mendonça França 2024-06-12 16:59:19 -04:00 committed by GitHub
commit 445529cc3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View File

@ -96,6 +96,14 @@ module ActiveRecord
end end
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 private
def run_in_transaction? def run_in_transaction?
use_transactional_tests && use_transactional_tests &&
@ -255,7 +263,7 @@ module ActiveRecord
def method_missing(method, ...) def method_missing(method, ...)
if fixture_sets.key?(method.name) if fixture_sets.key?(method.name)
_active_record_fixture(method, ...) active_record_fixture(method, ...)
else else
super super
end end
@ -269,14 +277,13 @@ module ActiveRecord
end end
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] if fs_name = fixture_sets[fixture_set_name.name]
access_fixture(fs_name, *fixture_names) access_fixture(fs_name, *fixture_names)
else else
raise StandardError, "No fixture set named '#{fixture_set_name.inspect}'" raise StandardError, "No fixture set named '#{fixture_set_name.inspect}'"
end end
end end
alias_method :fixture, :_active_record_fixture
def access_fixture(fs_name, *fixture_names) def access_fixture(fs_name, *fixture_names)
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload

View File

@ -611,7 +611,7 @@ class FixturesTest < ActiveRecord::TestCase
def test_fixture_method_and_private_alias def test_fixture_method_and_private_alias
assert_equal "The First Topic", topics(:first).title assert_equal "The First Topic", topics(:first).title
assert_equal "The First Topic", fixture(: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 end
def test_fixture_method_does_not_clash_with_a_test_case_method def test_fixture_method_does_not_clash_with_a_test_case_method