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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1050047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:24:04  来源:igfitidea点击:

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

ruby-on-railsrubytestingshoulda

提问by Ethan

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 时遇到了一些问题,它用于创建用于单元和其他测试的对象。我想转到控制台并运行不同的 Factory Girl 调用来检查发生了什么。例如,我想去那里做...

>> Factory(:user).inspect

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

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

$ script/console RAILS_ENV=test

$ 脚本/控制台 RAILS_ENV=test

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

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

I tried various requirecalls including one with the absolute path to test_helper.rbbut they fail similarly to this:

我尝试了各种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. Argh.

咕噜噜 啊。

回答by August Lilleaas

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.

运行script/console --help。您会注意到语法是script/console [environment],在您的情况下是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.

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

For Rails 3 and 4

对于 Rails 3 和 4

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

运行rails c testbundle exec如果当前应用程序环境需要此选项,请预先添加。

For Rails 5 and 6

对于 Rails 5 和 6

Run rails console -e test.

运行rails console -e test

回答by sivabudh

In Rails 3, just do rails console testor rails console productionor rails console development(which is the default).

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

回答by David Smith

script/console test

Should be all you need.

应该是你所需要的。

回答by Tovi Newman

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

rails c -e test

回答by Aamir

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 命令行

回答by PurpleHymanet

David Smith is correct, just do

大卫史密斯是对的,就去做

script/console test

The help command will show why this works:

help 命令将显示为什么会这样:

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

这是[环境]位。

回答by Nick Davies

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

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

    对于最近的 Rails 版本、bundle exec rails 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

回答by Simone Carletti

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

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

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

回答by anyavacy

Test Env

测试环境

rails console test # or just rails c test

Developement Env

开发环境

rails console # or just rails c

回答by Touseef Murtaza

Command to run rails console test environment is

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

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