如何在测试环境中运行Rails控制台并加载test_helper.rb?

时间:2022-12-22 23:08: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 console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...

背景:我对Thoughtbot的“工厂女孩”gem有一些问题,它用于创建用于单元和其他测试的对象。我想去控制台,运行不同的工厂女孩电话,看看发生了什么。例如,我想去那里有do…

>> Factory(:user).inspect

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

我知道您可以在不同的环境中运行控制台……

$ script/console RAILS_ENV=test

美元/控制台RAILS_ENV =测试脚本

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

但是当我这样做的时候,工厂类是不可用的。看起来好像是test_helper。rb没有加载。

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

我尝试了各种各样的require调用,其中包括带有test_helper的绝对路径的调用。但他们的失败与此类似:

$ 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.

嗯……。啊。

8 个解决方案

#1


170  

For Rails < 3.0

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.

我不确定您是否需要测试帮助器,或者测试环境是否为您做了这一点,但是通过这个命令,您至少应该能够成功地引导到测试env中。

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

作为旁注:脚本/中不同的二进制文件设置rails环境的方式不同确实有点奇怪。

For Rails 3 and 4

对于Rails 3和4

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

运行rails c测试。如果您在当前的应用程序环境中需要此功能,请使用Prepend bundle exec。

#2


59  

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

在Rails 3中,只需做Rails控制台测试或Rails控制台的生产或Rails控制台开发(这是默认的)。

#3


10  

script/console test

Should be all you need.

应该是你所需要的。

#4


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.

这是(环境)。

#5


3  

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

您可以指定控制台命令应该在其中操作的环境。

rails c [environment]

Examples

例子

1) For Staging

1)举办

rails c staging

2) For Production

2)生产

rails c production

For source & detailed description: The Rails Command Line

源代码和详细描述:Rails命令行

#6


1  

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

确保您安装了GEM,并在您的环境中添加了以下代码行。rb或测试。rb文件。

config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"

#7


1  

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.

    对于最近的Rails版本,bundle exec Rails c测试或替代语法。

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

    如何确保测试/test_helper函数。rb被加载到控制台会话中?

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

    类似的要求”。/test/test_helper应该这样做。

    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.

    对于我来说,这返回true,表明在启动控制台时还没有加载它。如果该语句返回false,那么您就浪费了一些击键,但是您仍然可以这样做。

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

    一旦test_helper加载完毕,如何调用其中定义的方法?

    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.

    在典型的test_helper中,自定义方法通常定义为ActiveSupport::TestCase的实例方法。如果你想调用其中一个,你需要一个类的实例。通过反复试验,ActiveSupport: TestCase。new有一个必需的参数,所以…通过它的东西。

    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

    如果test_helper有一个名为create_user的方法,可以这样调用:ActiveSupport::TestCase。new(“不知道这是什么”).create_user

#8


0  

Test Env

测试Env

rails console test # or just rails c test

Developement Env

发展Env

rails console # or just rails c

#1


170  

For Rails < 3.0

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.

我不确定您是否需要测试帮助器,或者测试环境是否为您做了这一点,但是通过这个命令,您至少应该能够成功地引导到测试env中。

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

作为旁注:脚本/中不同的二进制文件设置rails环境的方式不同确实有点奇怪。

For Rails 3 and 4

对于Rails 3和4

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

运行rails c测试。如果您在当前的应用程序环境中需要此功能,请使用Prepend bundle exec。

#2


59  

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

在Rails 3中,只需做Rails控制台测试或Rails控制台的生产或Rails控制台开发(这是默认的)。

#3


10  

script/console test

Should be all you need.

应该是你所需要的。

#4


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.

这是(环境)。

#5


3  

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

您可以指定控制台命令应该在其中操作的环境。

rails c [environment]

Examples

例子

1) For Staging

1)举办

rails c staging

2) For Production

2)生产

rails c production

For source & detailed description: The Rails Command Line

源代码和详细描述:Rails命令行

#6


1  

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

确保您安装了GEM,并在您的环境中添加了以下代码行。rb或测试。rb文件。

config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"

#7


1  

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.

    对于最近的Rails版本,bundle exec Rails c测试或替代语法。

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

    如何确保测试/test_helper函数。rb被加载到控制台会话中?

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

    类似的要求”。/test/test_helper应该这样做。

    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.

    对于我来说,这返回true,表明在启动控制台时还没有加载它。如果该语句返回false,那么您就浪费了一些击键,但是您仍然可以这样做。

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

    一旦test_helper加载完毕,如何调用其中定义的方法?

    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.

    在典型的test_helper中,自定义方法通常定义为ActiveSupport::TestCase的实例方法。如果你想调用其中一个,你需要一个类的实例。通过反复试验,ActiveSupport: TestCase。new有一个必需的参数,所以…通过它的东西。

    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

    如果test_helper有一个名为create_user的方法,可以这样调用:ActiveSupport::TestCase。new(“不知道这是什么”).create_user

#8


0  

Test Env

测试Env

rails console test # or just rails c test

Developement Env

发展Env

rails console # or just rails c