MiniTest 和 Rails 入门

发布于 2024-11-25 13:13:44 字数 1244 浏览 2 评论 0原文

我想从模型开始将现有的 Rails 应用程序从 rspec 切换到 minitest。因此我创建了一个文件夹test。在里面,我创建了一个名为 minitest_helper.rb 的文件,其中包含以下内容:

require "minitest/autorun"

ENV["RAILS_ENV"] = "test"

以及包含 forum_spec.rb 的文件夹 models

require "minitest_helper"

describe "one is really one" do
  before do
    @one = 1
  end

  it "must be one" do
    @one.must_equal 1
  end
end

现在我可以运行 < code>ruby -Itest test/models/forum_spec.rb 得到以下结果:

Loaded suite test/models/forum_spec
Started
.
Finished in 0.000553 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 12523

很好。但现在我想要加载环境,并将以下行添加到 minitest_helper.rb (从 rspec 的等效文件复制):

require File.expand_path("../../config/environment", __FILE__)

现在我再次运行它,结果如下:

Loaded suite test/models/forum_spec
Started

Finished in 0.001257 seconds.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 57545

测试和断言消失了。原因可能是什么?

系统信息:

  • ruby 1.9.2p180(2011-02-18 修订版 30909)[x86_64-darwin10.8.0]
  • Rails 3.1.0.rc4

I want to switch an existing rails application from rspec to minitest starting with the models. Therefore I created a folder test. Inside there I created a file named minitest_helper.rb with the following content:

require "minitest/autorun"

ENV["RAILS_ENV"] = "test"

and the folder models containing forum_spec.rb:

require "minitest_helper"

describe "one is really one" do
  before do
    @one = 1
  end

  it "must be one" do
    @one.must_equal 1
  end
end

Now I can run ruby -Itest test/models/forum_spec.rb with the following result:

Loaded suite test/models/forum_spec
Started
.
Finished in 0.000553 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 12523

That's nice. But now I want the environment to be loaded and I add the following line to minitest_helper.rb (copied from the equivalent file from rspec):

require File.expand_path("../../config/environment", __FILE__)

Now I run it again with the following result:

Loaded suite test/models/forum_spec
Started

Finished in 0.001257 seconds.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 57545

The Tests and assertions are gone. What could be the reason for that?

System Info:

  • ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.8.0]
  • Rails 3.1.0.rc4

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

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

发布评论

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

评论(1

鸵鸟症 2024-12-02 13:13:44

由于您要从 rspec 切换应用程序,因此您很可能在 Gemfile 中指定的测试环境中拥有 rspec gem,如下所示:

group :test do
  gem 'rspec'
end

当您使用 ENV["RAILS_ENV"] = "test",您正在加载 rspec,它定义了自己的 describe 方法并覆盖 minitest 定义的方法。

所以这里有2个解决方案:
1.从测试环境中删除rspec gem
2. 如果您在切换到 minitest 时仍想运行 rspecs,则可以保留“测试”环境并专门为 minitest 定义另一个测试环境。我们称之为 minitest - 将 config/environment/test.rb 复制到 config/enviroment/minitest.rb,为 minitest 环境定义数据库,并更新 minitest_helper 以将 RAILS_ENV 设置为“minitest”:(

$ cp config/environments/test.rb config/environments/minitest.rb

一部分)config/数据库.yml:

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

test/minitest_helper.rb:

ENV["RAILS_ENV"] = "minitest"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"

Since you are switching the app from rspec, you most probably have rspec gem in test environment specified in Gemfile, something like:

group :test do
  gem 'rspec'
end

When you load up the 'test' environment with ENV["RAILS_ENV"] = "test", you are loading up the rspec, which defines its own describe method and overrides the one defined by minitest.

So there are 2 solutions here:
1. Remove rspec gem from test environment
2. If you still want to run rspecs while switching to minitest, you can leave the 'test' environment alone and define another test environment specifically for minitest. Let's call it minitest - copy the config/environment/test.rb to config/enviroment/minitest.rb, define database for minitest environment, and update minitest_helper to set RAILS_ENV to 'minitest':

$ cp config/environments/test.rb config/environments/minitest.rb

(a portion of) config/database.yml:

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

test/minitest_helper.rb:

ENV["RAILS_ENV"] = "minitest"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文