Skip to content

Commit

Permalink
Fix clash with methods named fixture in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu committed Mar 19, 2024
1 parent 5411787 commit 461ac59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def load_instances?

def method_missing(method, ...)
if fixture_sets.key?(method.name)
fixture(method, ...)
_active_record_fixture(method, ...)
else
super
end
Expand All @@ -269,13 +269,14 @@ def respond_to_missing?(method, include_private = false)
end
end

def 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
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,31 @@ def test_fixtures
ensure
ENV["DATABASE_URL"] = db_url_tmp
end

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
end

def test_fixture_method_does_not_clash_with_a_test_case_method
test_case = Class.new(ActiveRecord::TestCase) do
fixtures :accounts

def test_fixtures
assert accounts(:signals37)
end

private
def fixture
Account.new
end
end

result = test_case.new(:test_fixtures).run

assert_predicate result, :passed?, "Expected #{result.name} to pass:\n#{result}"
end
end

class HasManyThroughFixture < ActiveRecord::TestCase
Expand Down

0 comments on commit 461ac59

Please sign in to comment.