mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Passing in no arguments to the dynamic fixture accessor method returns all fixtures, not an empty array.
This commit is contained in:
parent
e88e35804a
commit
06f45435da
3 changed files with 49 additions and 4 deletions
|
@ -1 +1,7 @@
|
|||
* When calling the dynamic fixture accessor method with no arguments it now returns all fixtures of this type.
|
||||
Previously this method always returned an empty array.
|
||||
|
||||
*Kevin McPhillips*
|
||||
|
||||
|
||||
Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md) for previous changes.
|
||||
|
|
|
@ -70,13 +70,32 @@ module ActiveRecord
|
|||
# test. To ensure consistent data, the environment deletes the fixtures before running the load.
|
||||
#
|
||||
# In addition to being available in the database, the fixture's data may also be accessed by
|
||||
# using a special dynamic method, which has the same name as the model, and accepts the
|
||||
# name of the fixture to instantiate:
|
||||
# using a special dynamic method, which has the same name as the model.
|
||||
#
|
||||
# test "find" do
|
||||
# Passing in a fixture name to this dynamic method returns the fixture matching this name:
|
||||
#
|
||||
# test "find one" do
|
||||
# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
|
||||
# end
|
||||
#
|
||||
# Passing in multiple fixture names returns all fixtures matching these names:
|
||||
#
|
||||
# test "find all by name" do
|
||||
# assert_equal 2, web_sites(:rubyonrails, :google).length
|
||||
# end
|
||||
#
|
||||
# Passing in no arguments returns all fixtures:
|
||||
#
|
||||
# test "find all" do
|
||||
# assert_equal 2, web_sites.length
|
||||
# end
|
||||
#
|
||||
# Passing in any fixture name that does not exist will raise <tt>StandardError</tt>:
|
||||
#
|
||||
# test "find by name that does not exist" do
|
||||
# assert_raise(StandardError) { web_sites(:reddit) }
|
||||
# end
|
||||
#
|
||||
# Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the
|
||||
# following tests:
|
||||
#
|
||||
|
@ -909,6 +928,8 @@ module ActiveRecord
|
|||
|
||||
define_method(accessor_name) do |*fixture_names|
|
||||
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
|
||||
return_single_record = fixture_names.size == 1
|
||||
fixture_names = @loaded_fixtures[fs_name].fixtures.keys if fixture_names.empty?
|
||||
|
||||
@fixture_cache[fs_name] ||= {}
|
||||
|
||||
|
@ -923,7 +944,7 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
instances.size == 1 ? instances.first : instances
|
||||
return_single_record ? instances.first : instances
|
||||
end
|
||||
private accessor_name
|
||||
end
|
||||
|
|
|
@ -95,6 +95,24 @@ class FixturesTest < ActiveRecord::TestCase
|
|||
assert_nil(topics["second"]["author_email_address"])
|
||||
end
|
||||
|
||||
def test_no_args_returns_all
|
||||
all_topics = topics
|
||||
assert_equal 5, all_topics.length
|
||||
assert_equal "The First Topic", all_topics.first["title"]
|
||||
assert_equal 5, all_topics.last.id
|
||||
end
|
||||
|
||||
def test_no_args_record_returns_all_without_array
|
||||
all_binaries = binaries
|
||||
assert_kind_of(Array, all_binaries)
|
||||
assert_equal 1, binaries.length
|
||||
end
|
||||
|
||||
def test_nil_raises
|
||||
assert_raise(StandardError) { topics(nil) }
|
||||
assert_raise(StandardError) { topics([nil]) }
|
||||
end
|
||||
|
||||
def test_inserts
|
||||
create_fixtures("topics")
|
||||
first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'David'")
|
||||
|
|
Loading…
Reference in a new issue