I have set up some fixtures in test/fixtures/users.xml: # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html admin: id: 1 username: admin password: admin pbarry: id: 2 username: pbarry I have a unit test for my user model: require File.dirname(__FILE__) + ''/../test_helper'' class UserTest < Test::Unit::TestCase fixtures :users def test_find user = User.find(@admin.id) assert_equals(@admin.id,user.id) end end When I run the test, I get this: C:\ruby\workspace\lms>ruby test/unit/user_test.rb Loaded suite test/unit/user_test Started E Finished in 0.156 seconds. 1) Error: test_find(UserTest): RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id test/unit/user_test.rb:7:in `test_find'' 1 tests, 0 assertions, 0 failures, 1 errors Why is @admin nil? Shouldn''t it be populated from the fixture? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060428/eb018ca7/attachment.html
Hello Paul, 2006/4/28, Paul Barry <mail@paulbarry.com>: [...]> def test_find > user = User.find(@admin.id) > assert_equals(@admin.id,user.id) > end > > end[...]> Why is @admin nil? Shouldn''t it be populated from the fixture?Because the instantiated fixtures are disabled by default. You shoud use sth like : admin = users(:admin) user = User.find(admin.id) assert_equals admin.id, user.id -- Jean-Fran?ois. -- ? la renverse.
Thanks, that did it. This must be one of those new to Rails 1.1 things? We desparately need a new version of Agile Web Dev with Rails updated for 1.1. I know the plan was to not "break the book", but I think there a number of these little details that have unfortunately done that. On 4/28/06, Jean-Fran?ois <jf.web3@gmail.com> wrote:> > Hello Paul, > > 2006/4/28, Paul Barry <mail@paulbarry.com>: > [...] > > def test_find > > user = User.find(@admin.id) > > assert_equals(@admin.id,user.id) > > end > > > > end > > [...] > > Why is @admin nil? Shouldn''t it be populated from the fixture? > > Because the instantiated fixtures are disabled by default. > You shoud use sth like : > > admin = users(:admin) > user = User.find(admin.id) > assert_equals admin.id, user.id > > -- Jean-Fran?ois. > > > -- > ? la renverse. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060428/8fe4adb2/attachment.html