Skip to content

Commit ef947f9

Browse files
committed
Expose a generic fixture accessor for fixture names that may conflict with Minitest
```ruby assert_equal "Ruby on Rails", web_sites(:rubyonrails).name assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name ``` This was brought to me by someone with a `Metadata` model. The fixtures accessor being `metadata` which conflicts with the `metadata` method recently added in `Minitest`.
1 parent 8bb5620 commit ef947f9

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

activerecord/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Expose a generic fixture accessor for fixture names that may conflict with Minitest
2+
3+
```ruby
4+
assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
5+
assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
6+
```
7+
8+
*Jean Boussier*
9+
110
* Using `Model.query_constraints` with a single non-primary-key column used to raise as expected, but with an
211
incorrect error message. This has been fixed to raise with a more appropriate error message.
312

activerecord/lib/active_record/fixtures.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ class FixtureClassNotFound < ActiveRecord::ActiveRecordError # :nodoc:
110110
# assert_raise(StandardError) { web_sites(:reddit) }
111111
# end
112112
#
113+
# If the model names conflicts with a +TestCase+ methods, you can use the generic +fixture+ accessor
114+
#
115+
# test "generic find" do
116+
# assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
117+
# end
118+
#
113119
# Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the
114120
# following tests:
115121
#

activerecord/lib/active_record/test_fixtures.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ def load_instances?
254254
end
255255

256256
def method_missing(method, ...)
257-
if fs_name = fixture_sets[method.name]
258-
access_fixture(fs_name, ...)
257+
if fixture_sets.key?(method.name)
258+
fixture(method, ...)
259259
else
260260
super
261261
end
@@ -269,6 +269,14 @@ def respond_to_missing?(method, include_private = false)
269269
end
270270
end
271271

272+
def fixture(fixture_set_name, *fixture_names)
273+
if fs_name = fixture_sets[fixture_set_name.name]
274+
access_fixture(fs_name, *fixture_names)
275+
else
276+
raise StandardError, "No fixture set named '#{fixture_set_name.inspect}'"
277+
end
278+
end
279+
272280
def access_fixture(fs_name, *fixture_names)
273281
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
274282
return_single_record = fixture_names.size == 1

0 commit comments

Comments
 (0)