Locating an arbitrary fixture


#1

Is there a good way to get an arbitrary fixture from the generated
fixture function?

I need a way to get one of my fixtures, I don’t care which one. It
doesn’t need to be random and could always return the same fixture.
This is part of a support function for custom assertions and it doesn’t
know what class its working on, let alone what fixture names are
possible.

I’ve tried digging around some in the foxture source file to see exactly
how they are implemented, but its not quite clear yet. I’ve been trying
to do something (very messy) like:
tmp =
@@already_loaded_fixtures[self.class][@klass.to_s.downcase.pluralize]

tmp is now a double nested array. However it appears to be protected
in some manner as tmp[0] returns nil. If I flatten the list, the
"protection’ is still present. I can’t access the elements by index. I
can’t get at them via tmp.values, etc. I can iterate via each: tmp.each
{|x| puts x.inspect } displays the contents. So it seems like I’m
definitly trying to get at something in a manner that I shouldn’t be.

All I want is to be able to basically do a
x = users(:some_user) without knowing either “users” or
“:some_user”… the first part is easy:
self.send(@klass.to_s.downcase.pluralize, …?). If I had a good way
to get a list of names within the fixture that would complete the
picture. Or is there some alternate way that I’m missing…

Any suggestions?

Eric


#2

Eric D. Nielsen wrote:

All I want is to be able to basically do a
x = users(:some_user) without knowing either “users” or
“:some_user”… the first part is easy:
self.send(@klass.to_s.downcase.pluralize, …?). If I had a good way
to get a list of names within the fixture that would complete the
picture. Or is there some alternate way that I’m missing…

Any suggestions?

Eric

You can use ActiveRecord finders in tests.

def foo(klass)
klass.find(1)
end

foo(User) #=> user object with id=1
foo(Post) #=> post object with id=1

That might work better that using named fixtures if you don;t care about
the names.


#3

Alex W. wrote:

Eric D. Nielsen wrote:

All I want is to be able to basically do a
x = users(:some_user) without knowing either “users” or
“:some_user”… the first part is easy:
self.send(@klass.to_s.downcase.pluralize, …?). If I had a good way
to get a list of names within the fixture that would complete the
picture. Or is there some alternate way that I’m missing…

Any suggestions?

Eric

You can use ActiveRecord finders in tests.

def foo(klass)
klass.find(1)
end

foo(User) #=> user object with id=1
foo(Post) #=> post object with id=1

That might work better that using named fixtures if you don;t care about
the names.

self.smack… such a simple answer to what I made into a complicated
mess. Thank you.

Eric