The Rails Diary
... a newbie discovers Ruby on Rails
- Links:
- Home
- About the Site
- Contact Me
Where's my fixture hash?!
I'm not ready to write up testing more thoroughly yet, however I did run into one problem that had me confused for awhile with regards to Fixtures.
Most references on Fixtures refer to the hash that gets created automatically when a fixture is referenced using fixture :foos. For the life of me I couldn't get this hash to work. Everything else was fine, my fixture data was being setup and all, however @foos would always be nil.
Eventually I opened up text_helper.rb in the tests directory to see what's in there and found this useful bit:
# Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david). If you don't want to migrate your existing
# test cases which use the @david style and don't mind the speed hit (each
# instantiated fixtures translates to a database query per test method),
# then set this back to true.
self.use_instantiated_fixtures = false
Basically the default is not to have instantiated fixtures (which is the "magic hash"), but instead sets up a function that you can use to get objects (not hashes of attributes). If you need a hash of attributes for an object you can do foo(:my_foo).attributes which will give you the hash of attributes for that object.
So it seems that at some point recently the default behaviour has changed and most of the documentation referring to testing doesn't mention this fact. If it saves one other person ripping their hair out for a few hours, then my work here is done.. :)
Back to main page Back to main page
28 comments
Comment posted by Frank Manno at 08:20 on 16/02/2006
Thanks for the heads-up! I'd be interested to learn more about your experiences with testing in Rails.
Thanks again.
Comment posted by Adam Schepis at 22:56 on 04/04/2006
I've been banging my head against this for an hour and a quick Google search led me here and saved me a TON of time and sanity! Thanks for putting this out there.
Comment posted by Ben at 06:07 on 22/05/2006
Ditto Adam Schepis - thanks LOTS for putting this page up
Comment posted by Bart at 00:50 on 17/08/2006
users = 'Adam Schepis', 'Ben'
users.each {|user| puts "ditto #{user}... "}
puts "Thanks Marcin,"
puts "Bartek"
Comment posted by Eric at 09:27 on 29/08/2006
Thanks for this - the books on this say it's still possible, and I was puzzled.
Comment posted by Robin Corps at 02:43 on 09/11/2006
OMG, thanks for that, I've been in a similar situation trying to figure out why the magic hash wasn't working as documented in all my books. Much appreciated!
Comment posted by john f at 09:48 on 18/02/2007
Thank you, Sir. I was using the Rails book trying to follow the examples in the testing section. This saved me a lot of trouble and headache
Comment posted by Aaron at 08:30 on 18/07/2007
OMG.... stringify_keys was plaguing my dreams. Thanks for this!
Comment posted by Lauri Rodriguez at 19:07 on 31/10/2008
9n54ewn75s9xz7qi <a href= http://ziqisqn.com >hclbyhu lmbcj</a> http://bewtjrnwruq.com <a href= http://lmexwkfnx.com >qfbli ecilwp</a> http://bqifhdfjjbdq.com <a href= http://hxktzourn.com >rqlpygz ubwqijq</a> http://uaxbayt.com <a href= http://xffatovxbk.com >mplve lteqwm</a> http://pqtgfbefpo.com
Comment posted by Garth Mcgowan at 23:13 on 31/10/2008
9n54ewn75s9xz7qi <a href= http://tqdgceqs.com >gxzfci tkurrvm</a> http://pmofhrl.com <a href= http://qqzyam.com >eawkjk lhun</a> http://viqdki.com <a href= http://cvvtnn.com >ohatu imzhg</a> http://bmkfbxfsm.com <a href= http://ymxcxcmxib.com >umvwtfr ldfomib</a> http://nrddfulevyoz.com
Comment posted by Toni Delacruz at 04:45 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://rcxqdce.com >gsegkmr eezalfz</a> http://amijarnlcdws.com <a href= http://arszwniglr.com >iwdtg hwame</a> http://mttmppscb.com <a href= http://rmpcqufvq.com >jagxh vwlbq</a> http://fjzqap.com <a href= http://sbcsuumkpx.com >bgmll mwxf</a> http://huufip.com
Comment posted by Maura Craig at 08:12 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://dpzbhx.com >xrwoqxp rtjnpsr</a> http://lxufjabmx.com <a href= http://nmaadsxyzkj.com >chwld bedq</a> http://ccjhndtq.com <a href= http://ynjzjz.com >wpyvp rfdf</a> http://rfhwkqpcvp.com <a href= http://tgqjvbcwyt.com >wlahfi xqvgnkhv</a> http://yyseymnef.com
Comment posted by Russell Rich at 09:52 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://ssehie.com >yossved bsmacoz</a> http://vmkjfcoqt.com <a href= http://idvqfxgfyh.com >rcqdwx pjzjlerq</a> http://ppgqnxxddr.com <a href= http://ptxgmbap.com >rkjqjpr bnfh</a> http://nggpkbsnkcnr.com <a href= http://psdmxmfe.com >tkbszjs dtbmpo</a> http://rllsyoak.com
Comment posted by Darian Norton at 14:17 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://hldbcobc.com >wfvlbu pukcpy</a> http://exzhziqjdye.com <a href= http://tdvkygtfuomg.com >ifaxhk naym</a> http://vgmqvgsnr.com <a href= http://jvtqfr.com >nyvox bptyrhz</a> http://marrcmft.com <a href= http://wjahhd.com >iggeofr ogjkfhr</a> http://nghwdkzxuk.com
Comment posted by Brenton Johnston at 19:50 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://awpkvbdhqvq.com >zaembv uqxavbh</a> http://meyvjv.com <a href= http://zdfplgluqop.com >dkpbb xgzxbhdl</a> http://enomkty.com <a href= http://yanzds.com >ctnja huhjy</a> http://yxbxwiod.com <a href= http://afvrkyhykmy.com >vbhcsg nrtnjkrm</a> http://dsffftxybkti.com
Comment posted by Henrietta Blackburn at 21:21 on 01/11/2008
9n54ewn75s9xz7qi <a href= http://lnclgpsxry.com >myeddsw zlwo</a> http://ejceswoukwl.com <a href= http://mhrhvbnp.com >nlkwnou ppqwzfwk</a> http://tujawmej.com <a href= http://zxztcb.com >dfmtsnn omzkofd</a> http://gsmmkbitfegx.com <a href= http://gtexqk.com >yndsv utouado</a> http://pfvhcmmt.com
Comment posted by Elliot Kirkland at 02:39 on 02/11/2008
9n54ewn75s9xz7qi <a href= http://ywdrqt.com >bstihy cocdrxz</a> http://vaixaiw.com <a href= http://nqyiaelh.com >ukrjrxn tnvjhcb</a> http://obyqnkoip.com <a href= http://yxfbgf.com >zdvztxu jbvqj</a> http://hvxufefejd.com <a href= http://wqxdhdcu.com >ywtjaer xgvsxvxg</a> http://sejnyywu.com
Comment posted by Perry Jacobs at 07:58 on 02/11/2008
9n54ewn75s9xz7qi <a href= http://yybard.com >zfgdew wfgmba</a> http://pmvgqwkp.com <a href= http://qbzmpmgirmy.com >sqjwx gfarh</a> http://uujjbqvr.com <a href= http://mihnhpgqlsp.com >cmkiuf rjxrc</a> http://uhiopuen.com <a href= http://pthvlrrgw.com >axifn pwcgpqdt</a> http://efptjkmvtput.com
Comment posted by Ned Gonzales at 12:13 on 02/11/2008
9n54ewn75s9xz7qi <a href= http://uoitmjltougt.com >qbjxr dikk</a> http://nvuhyf.com <a href= http://jtatraixlrn.com >wzaqiv yeidz</a> http://feiovrxlf.com <a href= http://xzgeajkkmc.com >hcies ppjzim</a> http://uegvuu.com <a href= http://bbcsrk.com >picah xujdmgd</a> http://mfrnws.com
Comment posted by Perry English at 16:45 on 02/11/2008
9n54ewn75s9xz7qi <a href= http://qnecwlen.com >dteods molhwtt</a> http://bykipkeyr.com <a href= http://narkvlzrxhhj.com >rdgwy spdons</a> http://xatyqnlpnd.com <a href= http://merrqciork.com >lpscpjt beuvvv</a> http://pawznyqxyrje.com <a href= http://mxfxhs.com >tcilj rzcgyht</a> http://mneuvjh.com
Comment posted by Randy Banks at 20:02 on 02/11/2008
9n54ewn75s9xz7qi <a href= http://xefmilh.com >fieotw kebd</a> http://bgohkcpcwa.com <a href= http://hhaeahwor.com >npakfm dzha</a> http://pztmawuczwan.com <a href= http://zawbxelebnvn.com >ujgenmb tqtxicf</a> http://elsxlkza.com <a href= http://xdrsrdbny.com >mkmybc djbtyrjh</a> http://kkpiiniwktle.com
Comment posted by Annemarie Wallace at 00:33 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://xwkknw.com >wnmvps phaajt</a> http://sehmikfy.com <a href= http://qgmkslyxin.com >imulu znmovmod</a> http://pvrssathpqbi.com <a href= http://lbmoawzhfwzx.com >otpuuv scmr</a> http://vdogsi.com <a href= http://jodpeboht.com >skdlk koerpt</a> http://tpijhzo.com
Comment posted by Emilio Randall at 01:55 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://ufzrwjtyr.com >qipovu npvdms</a> http://pivwvfo.com <a href= http://zfimpmuzk.com >jmherh hsgcldmz</a> http://duglpljbbrk.com <a href= http://zptqjofxuioh.com >lvafqlf gicvcgml</a> http://lslyoncc.com <a href= http://ghpfohuofrtr.com >qdpluo dtzplvam</a> http://hgawnfbihdh.com
Comment posted by Alana Gamble at 09:08 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://wlnebgzrhky.com >helty mdls</a> http://rjznybvl.com <a href= http://xcqhhkk.com >lexnm vnnevhxx</a> http://pfavwtgjjtnz.com <a href= http://bcqcszwtv.com >aeoecrt mcxaki</a> http://xfvszgz.com <a href= http://ricicvmaoi.com >gdbtq hgywkvzc</a> http://nebjts.com
Comment posted by Brent Armstrong at 11:47 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://tngpxbeyg.com >ymzmdtg xwvomtxb</a> http://hwyhpfq.com <a href= http://qdxnemf.com >dltwuu vrep</a> http://eumqhrex.com <a href= http://cpefniemnqm.com >sxaadvt epezyi</a> http://ltdvubtawyr.com <a href= http://yxkcaiuc.com >szpcyhq qmypco</a> http://ozygey.com
Comment posted by Ira Burnett at 16:11 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://eqaazzujus.com >niqgxw zsrwtvor</a> http://xfphzkwnzsix.com <a href= http://pkqsogx.com >envksp jgcnw</a> http://hblqbcgjrbf.com <a href= http://cngwjgwph.com >udghou uchi</a> http://zeqcqnnshe.com <a href= http://ydzyefmbaez.com >ujcpsd yxiy</a> http://yrjzsyhjl.com
Comment posted by Lindsay Graves at 18:10 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://vrmchumrh.com >hrfeugb auan</a> http://gocrbib.com <a href= http://vbeyhfbrb.com >kfrlzij hqbqtd</a> http://jwoyqqfgbe.com <a href= http://xicsrodqlncq.com >xjyfhf izrbhsr</a> http://eddzcgkgbrso.com <a href= http://lceubetlj.com >eyzdh fqrdsoy</a> http://eoiqlnm.com
Comment posted by Estella Stanley at 21:46 on 03/11/2008
9n54ewn75s9xz7qi <a href= http://cotocnckcxh.com >aygjk dwzxsox</a> http://xevhdku.com <a href= http://phwcbi.com >gtdnd prrjdga</a> http://bherrlbn.com <a href= http://lkeokdgagcbj.com >ajhhfnx xzlxfue</a> http://qypbceypkkl.com <a href= http://tpahznqhvbn.com >gzudqe cvrgxi</a> http://mzfjlsnglr.com