如何在测试环境中运行Rails控制台并加载test_helper.rb?
背景:我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于轨道< 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 isscript/console [environment]
, which in your case isscript/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
. Prependbundle exec
if you need this for the current app environment.For Rails 5 and 6
Run
rails console -e test
.在 Rails 3 中,只需执行
rails console test
或rails console production
或rails consoledevelopment
(这是默认设置)。In Rails 3, just do
rails console test
orrails console production
orrails console development
(which is the default).对于 Rails 5.2.0:“不推荐将环境名称作为常规参数传递,并将在下一个 Rails 版本中删除。请改用 -e 选项。”
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."
应该是你所需要的。
Should be all you need.
您可以指定控制台命令应运行的环境。
示例
1) 用于分期
2) 用于生产
用于源和环境 详细描述:Rails 命令行
You can specify the environment in which the console command should operate.
Examples
1) For Staging
2) For Production
For source & detailed description: The Rails Command Line
大卫史密斯是正确的,只需执行
帮助命令将显示为什么它有效:
它是[环境]位。
David Smith is correct, just do
The help command will show why this works:
It's the [environment] bit.
我分担提问者的痛苦。 这里实际上存在三个独立的问题,其中一些已得到解决,有些则没有:
如何在测试环境中启动控制台?
对于最新的 Rails 版本,
bundle execrails c test
或替代语法。如何确保 test/test_helper.rb 加载到该控制台会话中?
类似
require './test/test_helper'
应该可以做到。对我来说,这返回 true,表明当我启动控制台时它尚未加载。 如果该语句返回 false,那么您只是浪费了几次按键,但您仍然可以继续。
加载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:
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
确保您安装了 GEM 并在 environment.rb 或 test.rb 文件中添加了以下行。
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
测试环境
开发环境
Test Env
Developement Env
运行 Rails 控制台测试环境的命令是
,或者
如果您遇到类似的问题
,那么您应该首先通过运行来准备测试数据库
Command to run rails console test environment is
or
if you are facing problem something like
then you should first prepare your test DB by running