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 cons
In Rails 3, just do rails console test
or rails console production
or rails console development
(which is the default).
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
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"
script/console test
Should be all you need.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test
, or alternative syntaxes for that.
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.
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
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