在测试环境中启动轨道

发布于 2024-09-01 21:27:43 字数 2305 浏览 5 评论 0原文

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

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

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

这是我的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

我无法使用的

ruby script/server -e test

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

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

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

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

感谢您的任何帮助。

更新:

我现在正在加载 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

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

感谢迄今为止的帮助。

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:

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

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.

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.

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:

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

Thanks for the help thus far.

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

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

发布评论

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

评论(3

染火枫林 2024-09-08 21:27:43

这个命令为我做到了。我能够使用它的数据库加载测试环境。

$rails s -e test

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

$rails s -e test
耀眼的星火 2024-09-08 21:27:43

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

ruby script/server -e test

并且在 config/database.yml 中必须定义测试环境数据库,即:

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

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

ruby script/server -e test

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

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000
梦里人 2024-09-08 21:27:43

您知道可以从 Rake 运行单元测试、功能测试和集成测试,对吗?查看 rake -T test 的输出以了解具体情况。

如果您需要更多自定义内容,您可以创建自己的 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

将类似这样的内容 :environment 为您加载当前环境。然后,您可以在测试环境中运行您的任务,如下所示:RAILS_ENV=test rake custom_tests:run

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

In case you need something more custom, you can create your own Rake task. Put something like this in a file in 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 => :environment loads the current environment for you. Then, you can run your task in the test environment like this: RAILS_ENV=test rake custom_tests:run

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