Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling fixture accessor method with no arguments returns all fixtures rather than empty array #28692

merged 1 commit into from Apr 7, 2017


Copy link

@kmcphillips kmcphillips commented Apr 7, 2017


Fixtures provide a convenience method to access fixtures by name. So given:

# fixtures/things.yml
  name: The first
  name: The second

You can access the Things using things():

# #<Thing:0x007f821b6346c0 name: "The second">
things(:one, :two)
# [#<Thing:0x007f821b6346c8 name: "The first">, #<Thing:0x007f821b6346c0 name: "The second">]

The inconsistent behaviour is that passing no arguments to this method always returns an empty array:

# []

The reason this happens is that it initializes an empty array of results, then iterates over all names passed in and pushes them into that array. Any names that do not match raise StandardError, but we don't check for empty arguments.


We now return all records for this fixture if none are passed in:

# [#<Thing:0x007f821b6346c8 name: "The first">, #<Thing:0x007f821b6346c0 name: "The second">]

There is a special case for if there is only a single fixture that exists, and was call with no arguments, we still return the array with one element. Where as if we ask for a single fixture by name we return it without wrapping it in an array.

Alternative solution

A simpler solution would be:

raise StandardError, "Must pass in a name" if fixture_names.blank?

But the "return all" solution exposes a nice iteration functionality that could be useful.


Copy link

rails-bot commented Apr 7, 2017

r? @sgrif

(@rails-bot has picked a reviewer for you, use r? to override)

Copy link
Contributor Author

kmcphillips commented Apr 7, 2017

Worth noting that pushing all names into fixture_names rather than just early existing with @loaded_fixtures[fs_name].fixtures.values causes them to be cached by following the existing code path.

@rafaelfranca rafaelfranca merged commit 2807986 into rails:master Apr 7, 2017
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

5 participants