Topic: How to load fixtures in console in test environment?

Hi!

I've got a test that fails and I'd like to execute it step by step in the console (yeah, I know I could probably debug it somehow, didn't really try to debug in RadRails yet smile).

How can I load fixtures in the console, to get the same data as in the test?

Last edited by g0nzo (2007-09-28 05:52:46)

Re: How to load fixtures in console in test environment?

Try loading fixtures in the test environment and then launching script/console in that environment like this:

rake db:fixtures:load RAILS_ENV=test
script/console test

Railscasts - Free Ruby on Rails Screencasts

Re: How to load fixtures in console in test environment?

Thanks!

Re: How to load fixtures in console in test environment?

After following these instructions I still don't seem to be able to use fixtures from the console.

I'm a beginner trying to learn rails and I think would help my understanding to be able to run the test code in the console.

I've got a fixture file test/fixtures/users.yml that contains:

patrick:
    login: patrick
    password: sekrit
    name: Patrick Lenz
    email: patrick@limited-overload.de

john:
    login: john
    password: gh752px
    name: John Doe
    email: john@doe.com


From the console as suggested above I do:
rake db:fixtures:load RAILS_ENV=test
script/console test

but I still don't seem to be able to use the fixture from the console.  Here is a paste from my console:
    [08:55:31 ~/htdocs/shovell] rake db:fixtures:load RAILS_ENV=test
    (in /Applications/mampstack-0.9.4/apache2/htdocs/shovell)
    [08:55:48 ~/htdocs/shovell] script/console test
    Loading test environment (Rails 2.1.0)
    >> users
    NameError: undefined local variable or method `users' for #<Object:0x389a0>
        from (irb):1

This is running in the tutorial "shovell" application from the "Simply Rails 2" book and the tests that use this fixture do run and pass.

Can anyone tell me how to use a fixture from the console or point me to a good learning resource for working with test code in the console?

Re: How to load fixtures in console in test environment?

Just because you're loading the fixtures in your database doesn't mean you can reference them like you would in a test. You can do something like User.find_by_login("patrick") to verify that your fixtures have been loaded.

Visit http://www.railway.at for quality Rails contract work. We also blog about Rails, AJAX and other things.

Re: How to load fixtures in console in test environment?

Thanks Clemens. 

So all the rake command does is load the database and starting the console with 'test' tells it to use that db. 

I was hoping for a way to have the console environment the same as the environment that the tests run in, and you could cut and paste code between the test and the console and if it worked in one it would work in the other.   This post is just about loading the right data into the db, thanks for making that clear.