Calling fixture accessor method with no arguments returns all fixtures rather than empty array #28692
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Fixtures provide a convenience method to access fixtures by name. So given:
You can access the
Thing
s usingthings()
: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.Solution
We now return all records for this fixture if none are passed in:
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:
But the "return all" solution exposes a nice iteration functionality that could be useful.
@rafaelfranca