Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
Calling fixture accessor method with no arguments returns all fixtures rather than empty array #28692
Fixtures provide a convenience method to access fixtures by name. So given:
# fixtures/things.yml one: name: The first two: name: The second
You can access the
things(:two) # #<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:
things # 
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
We now return all records for this fixture if none are passed in:
things # [#<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.
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.