Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you run a test in rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do I run a command in rails?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .


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.


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


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

script/console test

Should be all you need.


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