How to run Rails console in the test environment and load test_helper.rb?
Asked Answered
C

10

137

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...

>> Factory(:user).inspect

I know that you can run the console in different environments...

$ script/console RAILS_ENV=test

But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.

I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:

$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
  Errno::ENOENT: No such file or directory - 
  /Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb

Grr. Argh.

Chiu answered 26/6, 2009 at 16:29 Comment(1)
Consequently if you had placed the RAILS_ENV=test before script/console, it would have worked as you would expect.Sprag
H
220

For Rails < 3.0

Run script/console --help. You'll notice that the syntax is script/console [environment], which in your case is script/console test.

I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.

As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.

For Rails 3 and 4

Run rails c test. Prepend bundle exec if you need this for the current app environment.

For Rails 5 and 6

Run rails console -e test.

Hamlen answered 26/6, 2009 at 16:36 Comment(3)
In rails 3, it's just rails console [environment]Desired
It's a little inconsistent because to start the server you type rails server -e testDiez
In Rails 5 this still works. And, you do need to load at least some parts of your environment -- in my case features/support/helpers.rb wasn't automagically loaded. Also, Rack::Test isn't loaded.Jagannath
R
62

In Rails 3, just do rails console test or rails console production or rails console development (which is the default).

Reproach answered 8/7, 2011 at 20:57 Comment(0)
B
15

For Rails 5.2.0: "Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead."

rails c -e test
Berthaberthe answered 3/11, 2018 at 2:30 Comment(0)
R
10
script/console test

Should be all you need.

Rapeseed answered 26/6, 2009 at 18:24 Comment(0)
C
4

You can specify the environment in which the console command should operate.

rails c [environment]

Examples

1) For Staging

rails c staging

2) For Production

rails c production

For source & detailed description: The Rails Command Line

Chian answered 19/9, 2016 at 7:45 Comment(0)
M
3

David Smith is correct, just do

script/console test

The help command will show why this works:

$ script/console -h
Usage: console [environment] [options]
    -s, --sandbox                    Rollback database modifications on exit.
        --irb=[irb]                  Invoke a different irb.
        --debugger                   Enable ruby-debugging for the console.

It's the [environment] bit.

Maice answered 9/2, 2011 at 19:44 Comment(0)
M
2

I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:

  1. How do you start the console in the test environment?

    For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.

  2. How do you ensure that test/test_helper.rb is loaded into that console session?

    Something like require './test/test_helper' ought to do it.

    For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.

  3. Once test_helper is loaded, how do you call the methods defined in it?

    In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.

    If your test_helper has a method called create_user, you could invoke it this way: ActiveSupport::TestCase.new("no idea what this is for").create_user

Munificent answered 19/5, 2017 at 19:15 Comment(2)
Rather than trial or error - use the rails API guide to find what it needs api.rubyonrails.org/classes/ActiveSupport/TestCase.html ... it's probably test order which is :random by defaultHonorary
There's only those two class methods described for ActiveSupport::TestCase, so I'm unclear on what class/module it inherits its initialize method from. But it looks like the parameter it expects gets stored as @NAME on the created object.Munificent
C
1

Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.

config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Cybill answered 26/6, 2009 at 16:54 Comment(0)
M
0

Test Env

rails console test # or just rails c test

Developement Env

rails console # or just rails c
Marteena answered 14/4, 2016 at 7:30 Comment(0)
B
0

Command to run rails console test environment is

rails c -e test

or

RAILS_ENV=test rails c

if you are facing problem something like

ActiveRecord::StatementInvalid:
   Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`

then you should first prepare your test DB by running

bundle exec rake db:test:prepare
Bellbottoms answered 18/4, 2019 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.