On 9/21/05, Noah Davis <noah.box-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Being the good little coder that I am (good as in well-behaved of
> course), last night I decided I would start taking advantage of the
> nice little testing framework. So, I created a fixture. My first
> fixture was for a table named "tournies". That seemed to work ok
-- it
> dumped all that data into the database. Nice. Then I got to the unit
> test itself. A simple little one liner will do for my first unit test,
> I thought. Ok. So I grabbed one of the nice automatically created
> instance variables and asserted it equal to some value -- d''oh.
Nil,
> nil, nil. This drove me mad for a little.
>
> Turns out, fixtures aren''t as friendly as ActiveRecord::Base when
it
> comes to table to object translations, which surprised me somewhat.
> Usually, the rails theory goes, makes some smart decisions about
> defaults, but let your users have ultimate say.
>
> Anyhow, I presume what''s going on is that Fixtures looked for a
model
> named something like "Tourny" or "Tournie" but not
"Tourney" which is
> how my model is actually named, and hence was unable to translate it.
> I dug into the Fixture code and it seems the the logic is to sink or
> swim with "Inspector.classify" on the tablename.
>
> This, of course, isn''t the end of the world by any means -- as far
as
> failures go I''d much rather have it fail on instantiation than on
db
> insertion -- but this gotcha should probably be noted somewhere in the
> docs. The easy work around is to just call some setup code to
> instantiate these models manually.
>
> PS - So it''s not a very friendly subject line, but hey,
it''s gotta be
> the first Godfather reference on this list.
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
''tournies'' looks for Tourny
Tourney maps to ''tourneys'' by default.
You can use script/console to try this out, or
http://nubyonrails.topfunky.com/tools/pluralize if you don''t happen to
know the string inflector methods.
''tournies''.classify
''Tourney''.tabelize
I have an experimental patch for this
(http://dev.rubyonrails.com/ticket/2224) that allows you to put model
names in there. This works well for models with different table names
(such as Tourney using the ''tournies'' table) and for models
that live
in a module.
For instance, if you do fixtures :posts, it looks for the Post model.
It won''t find a MyApp::Post (which is an issue if you test code in
components).
--
rick
http://techno-weenie.net