Frank Kim
2005-Nov-23 03:11 UTC
unit tests don''t work where model name is different from table name
I have a model called Group. It uses the table ''covenant_groups''. When I ran a unit test it complained it couldn''t find the groups table. So I renamed the fixture file from "groups.yml" to "covenant_groups.yml" and changed the fixtures line in my unit test to "fixtures :covenant_groups". However now I can''t access anything from my fixture, it always complains it''s null. For example, covenant_groups(:allstars) always returns null when it should return a Group object. Does anyone know why this is happening? Is this a fixtures bug? -- Frank Kim http://meetingkoreans.com http://betweengo.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jeremy Kemper
2005-Nov-23 03:36 UTC
Re: unit tests don''t work where model name is different from table name
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 22, 2005, at 7:11 PM, Frank Kim wrote:> I have a model called Group. It uses the table ''covenant_groups''. > > When I ran a unit test it complained it couldn''t find the groups > table. So I renamed the fixture file from "groups.yml" to > "covenant_groups.yml" and changed the fixtures line in my unit test > to "fixtures :covenant_groups". > > However now I can''t access anything from my fixture, it always > complains it''s null. For example, covenant_groups(:allstars) > always returns null when it should return a Group object. > > Does anyone know why this is happening? Is this a fixtures bug? >Hmm. Is your covenant_groups.yml valid YAML? Try $ ruby -ryaml -e "puts YAML.load_file(''test/fixtures/ covenant_groups.yml'').inspect" Does your Group model work in ./script/console? $ ./script/console > Group.find(:all) jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDg+PWAQHALep9HFYRAs/xAKDEUVdRTyMFY/1Y8gMV13M62zP0PwCbBJGM 1g2KCCoSGMygAFepp4qr2n0=N/4f -----END PGP SIGNATURE-----
Matthew Thill
2005-Nov-23 04:21 UTC
Re: unit tests don''t work where model name is different from table name
You can''t just overwrite table_name in active record. You have to use set_table_name now. Why? I don''t know. This is something new. I did see that the example in the documentation shows the set_table_name method. ModelClass < ActiveRecord::Base set_table_name "different_table" end This will not work: ModelClass < ActiveRecord::Base def table_name "different_table" end end Frank Kim wrote:> I have a model called Group. It uses the table ''covenant_groups''. > > When I ran a unit test it complained it couldn''t find the groups table. So > I renamed the fixture file from "groups.yml" to "covenant_groups.yml" and > changed the fixtures line in my unit test to "fixtures :covenant_groups". > > However now I can''t access anything from my fixture, it always complains > it''s null. For example, covenant_groups(:allstars) always returns null when > it should return a Group object. > > Does anyone know why this is happening? Is this a fixtures bug? > > -- > Frank Kim > http://meetingkoreans.com > http://betweengo.com > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Frank Kim
2005-Nov-23 20:08 UTC
Re: unit tests don''t work where model name is different from table name
Hi Jeremy, I am confident covenant_groups.yml is valid because it writes into the database the expected values. My actual application also works correctly, it interacts correctly with the Group model which uses the covenant_groups table. I think this must be a fixtures bug. What do you think? -Frank On 11/22/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > > On Nov 22, 2005, at 7:11 PM, Frank Kim wrote: > > > I have a model called Group. It uses the table ''covenant_groups''. > > > > When I ran a unit test it complained it couldn''t find the groups > > table. So I renamed the fixture file from "groups.yml" to > > "covenant_groups.yml" and changed the fixtures line in my unit test > > to "fixtures :covenant_groups". > > > > However now I can''t access anything from my fixture, it always > > complains it''s null. For example, covenant_groups(:allstars) > > always returns null when it should return a Group object. > > > > Does anyone know why this is happening? Is this a fixtures bug? > > > > Hmm. Is your covenant_groups.yml valid YAML? Try > $ ruby -ryaml -e "puts YAML.load_file(''test/fixtures/ > covenant_groups.yml'').inspect" > > Does your Group model work in ./script/console? > $ ./script/console > > Group.find(:all) > > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDg+PWAQHALep9HFYRAs/xAKDEUVdRTyMFY/1Y8gMV13M62zP0PwCbBJGM > 1g2KCCoSGMygAFepp4qr2n0> =N/4f > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jeremy Kemper
2005-Nov-23 20:56 UTC
Re: unit tests don''t work where model name is different from table name
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 23, 2005, at 12:08 PM, Frank Kim wrote:> I am confident covenant_groups.yml is valid because it writes into > the database the expected values. My actual application also works > correctly, it interacts correctly with the Group model which uses > the covenant_groups table. > > I think this must be a fixtures bug. What do you think?Sorry; I wasn''t thinking. We can''t look up the class for a custom table name. covenant_groups(:some_name) is trying to load an instance of the CovenantGroup class, which doesn''t exist. This is expected, since you can''t specify the class for a fixtures table. It will change after 1.0. In the meantime, try CovenantGroup = Group at the top of your test file. Then the fixture will map covenant_groups table to ConvenantGroup class, so covenant_groups(:foo) should work. But, untested :) jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDhNeFAQHALep9HFYRAi6rAJ4iw4QafT5zgrsmPUcD2EvHW4Ci2gCgxMzw SqqRs5n+E4RG/ldQ1Y2a2L4=Z6Y7 -----END PGP SIGNATURE-----