如何在测试环境中运行

如何在测试环境中运行

本文介绍了如何在测试环境中运行 Rails 控制台并加载 test_helper.rb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我在使用 Thoughtbot 的工厂女孩" gem 时遇到了一些问题,它用于创建用于单元和其他测试的对象.我想转到控制台并运行不同的 Factory Girl 调用来检查发生了什么.例如,我想去那里做...

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

$ 脚本/控制台 RAILS_ENV=test

$ script/console RAILS_ENV=test

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

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

我尝试了各种 require 调用,包括一个带有 test_helper.rb 绝对路径的调用,但它们都失败了:

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

老大.啊.

推荐答案

For Rails 3.0

运行 script/console --help.您会注意到语法是 script/console [environment],在您的情况下是 script/console test.

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.

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

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

对于 Rails 3 和 4

运行rails c test.如果当前应用程序环境需要它,请预先添加 bundle exec.

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

对于 Rails 5 和 6

运行rails console -e test.

这篇关于如何在测试环境中运行 Rails 控制台并加载 test_helper.rb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:10