Ruby-on-rails 在测试环境中启动 rails

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2814901/
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 22:39:57  来源:igfitidea点击:

starting rails in test environment

ruby-on-railsenvironment

提问by Brian DiCasa

I'm trying to load up rails in the test environment using a ruby script. I've tried googling a bit and found this recommendation:

我正在尝试使用 ruby​​ 脚本在测试环境中加载 rails。我试过谷歌搜索了一下,发现这个建议:

require "../../config/environment"
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'test'

This seems to load up my environment alright, but my development database is still being used. Am I doing something wrong?

这似乎可以加载我的环境,但是我的开发数据库仍在使用中。难道我做错了什么?

Here is my database.yml file... however I don't think it is the issue

这是我的 database.yml 文件......但是我认为这不是问题

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_development
  pool: 5
  username: root
  password: dev
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_test
  pool: 5
  username: root
  password: dev
  host: localhost

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_production
  pool: 5
  username: root
  password: dev
  host: localhost

I can't use

我不能用

ruby script/server -e test

because I'm trying to run ruby code after I load rails. More specifically what I'm trying to do is: run a .sql database script, load up rails and then run automated tests. Everything seems to be working fine, but for whatever reason rails seems to be loading in the development environment instead of the test environment.

因为我在加载 rails 后尝试运行 ruby​​ 代码。更具体地说,我想要做的是:运行 .sql 数据库脚本,加载 Rails,然后运行自动化测试。一切似乎都运行良好,但无论出于何种原因,rails 似乎都加载到了开发环境而不是测试环境中。

Here is a shortened version of the code I am trying to run:

这是我尝试运行的代码的缩短版本:

system "execute mysql script here"

require "../../config/environment"
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'test'

describe Blog do
  it "should be initialized successfully" do
    blog = Blog.new
  end
end

I don't need to start a server, I just need to load my rails code base (models, controllers, etc..) so I can run tests against my code.

我不需要启动服务器,我只需要加载我的 Rails 代码库(模型、控制器等),这样我就可以对我的代码运行测试。

Thanks for any help.

谢谢你的帮助。

UPDATE:

更新:

I have my rails environment now loading. Now I'm trying to run my test files within my rake task. Here is my code:

我现在正在加载我的 rails 环境。现在我正在尝试在我的 rake 任务中运行我的测试文件。这是我的代码:

require"spec"
require "spec/rake/spectask"
RAILS_ENV = 'test'

namespace :run_all_tests do
  desc "Run all of your tests"

  puts "Reseting test database..."
  system "mysql --user=root --password=dev < C:\Brian\Work\Personal\BrianSite\database\BrianSite_test_CreateScript.sql"
  puts "Filling database tables with test data..."
  system "mysql --user=root --password=dev < C:\Brian\Work\Personal\BrianSite\database\Fill_Test_Tables.sql"

  puts "Starting rails test environment..."
  task :run => :environment do
    puts "RAILS_ENV is #{RAILS_ENV}"
    require "spec/models/blog_spec.rb"
  end
end

I thought the require "spec/models/blog_spec.rb" file would do it, but it doesn't seem to run the tests...

我认为需要“spec/models/blog_spec.rb”文件可以做到,但它似乎没有运行测试......

Thanks for the help thus far.

感谢您到目前为止的帮助。

采纳答案by agregtheitroade

You know you can run your unit, functional and integration tests from Rake, right ? Check out the output of rake -T testto see how.

你知道你可以从 Rake 运行你的单元、功能和集成测试,对吧?检查输出rake -T test以了解如何。

In case you need something more custom, you can create your own Rake task. Put something like this in a file in lib/tasks:

如果您需要更多自定义内容,您可以创建自己的 Rake 任务。把这样的东西放在一个文件中lib/tasks

namespace :custom_tests do
  desc "Run my custom tests"
  task :run => :environment do
    puts "RAILS_ENV is #{RAILS_ENV}"
    system "execute mysql script here"
    # Do whatever you need to do
  end
end

The => :environmentloads the current environment for you. Then, you can run your task in the test environment like this: RAILS_ENV=test rake custom_tests:run

=> :environment载荷为你当前的环境。然后,您可以在测试环境中运行您的任务,如下所示:RAILS_ENV=test rake custom_tests:run

回答by Ananth

This command did it for me. I was able to load up the test env aloong with it's Database.

这个命令为我做了。我能够加载测试环境及其数据库。

$rails s -e test

回答by fl00r

to start test environment you should run script/server with -eparam:

要启动测试环境,您应该使用-e参数运行脚本/服务器:

ruby script/server -e test

and in your config/database.yml must be defenition of test env database, i.e.:

并且在您的 config/database.yml 中必须是 test env 数据库的 defenition,即:

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000