Michael M
2005-Oct-27 18:19 UTC
Fixtures are not loading into instance variables. Why not?
Hi, I have a fixture in a unit test and I see that the fixture is loaded correctly into the database table, but no instance variables are created. So now I''m stuck. More background: this is my first RoR project and I have just installed the SaltedLoginGenerator and generated its output. Running the unit tests gives an error, so I cut it down to just 1 test: ------------------------------ require File.dirname(__FILE__) + ''/../test_helper'' class UserTest < Test::Unit::TestCase fixtures :users def test_auth p @bob p @users assert_equal @bob, User.authenticate("bob", "atest") end end ------------------------------ Note that the 2 print statements both produce nil. The assert statement fails because the User.authenticate method DOES retrieve the correct object from the database (it''s shown by the failed assertion) and compares it to nil. My conclusion: the fixture is correctly loaded in the database, but no instance variables are being created. This is where I''m stuck. I am on the latest version of Rails. Thanks, Mike __________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
Michael M
2005-Oct-27 18:44 UTC
Re: Fixtures are not loading into instance variables. Why not?
I found the answer here: http://www.clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting --- Michael M <spmikedev-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a fixture in a unit test and I see that the > fixture is loaded correctly into the database table, > but no instance variables are created. > So now I''m stuck. > > More background: this is my first RoR project and I > have just installed the SaltedLoginGenerator and > generated its output. > Running the unit tests gives an error, so I cut it > down to just 1 test: > > ------------------------------ > require File.dirname(__FILE__) + ''/../test_helper'' > class UserTest < Test::Unit::TestCase > fixtures :users > def test_auth > p @bob > p @users > assert_equal @bob, User.authenticate("bob", > "atest") > end > end > ------------------------------ > > Note that the 2 print statements both produce nil. > The assert statement fails because the > User.authenticate method DOES retrieve the correct > object from the database (it''s shown by the failed > assertion) and compares it to nil. > > My conclusion: the fixture is correctly loaded in > the > database, but no instance variables are being > created. > > This is where I''m stuck. > I am on the latest version of Rails. > > Thanks, > Mike > > > > > __________________________________ > Yahoo! Mail - PC Magazine Editors'' Choice 2005 > http://mail.yahoo.com >__________________________________ Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
Jeremy Kemper
2005-Oct-27 19:06 UTC
Re: Fixtures are not loading into instance variables. Why not?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 27, 2005, at 11:19 AM, Michael M wrote:> I have a fixture in a unit test and I see that the > fixture is loaded correctly into the database table, > but no instance variables are created. > So now I''m stuck.Hey Mike, in Rails 1.0 fixtures instantiation is turned off by default. You can reference your user bob as users(:bob), which loads bob from the database on-demand. The old way pre-loaded @bob from the database, slowing down all the tests that don''t use him. If you want the old behavior, you can do self.use_instantiated_fixtures = true at the beginning of your test class. But working with users(:bob) rather than @bob is recommended! Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDYSVOAQHALep9HFYRAvKKAJwKjTAVuGcJHTLSdRjcKfa+ZTczHACcCixn J5EJdki0s4bsEkzjw3Ddg+o=AK7R -----END PGP SIGNATURE-----