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

后端 未结 10 676
梦如初夏
梦如初夏 2020-12-12 19:58

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

相关标签:
10条回答
  • 2020-12-12 20:30

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

    0 讨论(0)
  • 2020-12-12 20:31

    Test Env

    rails console test # or just rails c test
    

    Developement Env

    rails console # or just rails c
    
    0 讨论(0)
  • 2020-12-12 20:33

    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"
    
    0 讨论(0)
  • 2020-12-12 20:35
    script/console test
    

    Should be all you need.

    0 讨论(0)
  • 2020-12-12 20:35

    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

    0 讨论(0)
  • 2020-12-12 20:38

    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

    0 讨论(0)
提交回复
热议问题