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

发布于 2024-07-25 03:08:00 字数 694 浏览 10 评论 0原文

背景:我在使用 Thoughtbot 的“Factory Girl”gem 时遇到一些问题,该 gem 用于创建在单元和其他测试中使用的对象。 我想转到控制台并运行不同的 Factory Girl 调用来查看发生了什么。 例如,我想进去做...

>> Factory(:user).inspect

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

$ script/console RAILS_ENV=test

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

我尝试了各种 require 调用,其中包括一个带有 test_helper.rb 绝对路径的调用,但它们失败的情况与此类似:

$ 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。 啊。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

﹎☆浅夏丿初晴 2024-08-01 03:08:00

对于轨道< 3.0

运行script/console --help。 您会注意到语法是 script/console [environment],在您的例子中是 script/console test

我不确定您是否必须需要测试助手,或者测试环境是否为您做到这一点,但使用该命令您至少应该能够成功启动到测试环境。

作为旁注: script/ 中的各种二进制文件具有不同的设置 Rails 环境的方式,这确实有点奇怪。

对于 Rails 3 和 4

运行 rails c test。 如果当前应用环境需要此操作,请在前面添加 bundle exec

对于 Rails 5 和 6

运行 rails console -e test

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.

冬天旳寂寞 2024-08-01 03:08:00

在 Rails 3 中,只需执行 rails console testrails console productionrails consoledevelopment (这是默认设置)。

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

安穩 2024-08-01 03:08:00

对于 Rails 5.2.0:“不推荐将环境名称作为常规参数传递,并将在下一个 Rails 版本中删除。请改用 -e 选项。”

rails c -e test

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
这个俗人 2024-08-01 03:08:00
script/console test

应该是你所需要的。

script/console test

Should be all you need.

私野 2024-08-01 03:08:00

您可以指定控制台命令应运行的环境。

rails c [environment]

示例

1) 用于分期

rails c staging

2) 用于生产

rails c production

用于源和环境 详细描述:Rails 命令行

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

千纸鹤 2024-08-01 03:08:00

大卫史密斯是正确的,只需执行

script/console test

帮助命令将显示为什么它有效:

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

它是[环境]位。

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.

空城缀染半城烟沙 2024-08-01 03:08:00

我分担提问者的痛苦。 这里实际上存在三个独立的问题,其中一些已得到解决,有些则没有:

  1. 如何在测试环境中启动控制台?

    对于最新的 Rails 版本,bundle execrails c test 或替代语法。

  2. 如何确保 test/test_helper.rb 加载到该控制台会话中?

    类似 require './test/test_helper' 应该可以做到。

    对我来说,这返回 true,表明当我启动控制台时它尚未加载。 如果该语句返回 false,那么您只是浪费了几次按键,但您仍然可以继续。

  3. 加载test_helper后,如何调用其中定义的方法?

    在典型的 test_helper 中,自定义方法通常定义为 ActiveSupport::TestCase 的实例方法。 因此,如果您想调用其中一个,则需要该类的一个实例。 经过反复试验,ActiveSupport::TestCase.new 有一个必需的参数,所以...传递一些东西。

    如果你的 test_helper 有一个名为 create_user 的方法,你可以这样调用它:
    ActiveSupport::TestCase.new("no idea what this is for").create_user

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

掌心的温暖 2024-08-01 03:08:00

确保您安装了 GEM 并在 environment.rb 或 test.rb 文件中添加了以下行。

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

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"
又怨 2024-08-01 03:08:00

测试环境

rails console test # or just rails c test

开发环境

rails console # or just rails c

Test Env

rails console test # or just rails c test

Developement Env

rails console # or just rails c
岁月蹉跎了容颜 2024-08-01 03:08:00

运行 Rails 控制台测试环境的命令是

rails c -e test

,或者

RAILS_ENV=test rails c

如果您遇到类似的问题

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

,那么您应该首先通过运行来准备测试数据库

bundle exec rake db:test:prepare

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文