【问题标题】:Getting Started with MiniTest and RailsMiniTest 和 Rails 入门
【发布时间】:2011-10-11 19:26:32
【问题描述】:

我想从模型开始将现有的 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

现在我可以运行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

【问题讨论】:

  • 您是否尝试过创建一个新的测试应用程序并复制它创建的文件? Rails 3.0.5 创建的test_helper.rb(这是我现在机器上的)看起来和你的不一样。
  • @s.m.不同之处在于最后缺少require 'rails/test_help'(以及对固定装置的支持)。添加了,没有改变。
  • +1 个非常详细的问题!
  • 第一种情况下环境不是已经加载了吗?

标签: ruby-on-rails ruby ruby-1.9.2 minitest


【解决方案1】:

由于您是从 rspec 切换应用程序,因此您很可能在 Gemfile 中指定的测试环境中拥有 rspec gem,类似于:

group :test do
  gem 'rspec'
end

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

所以这里有两种解决方案: 1. 从测试环境中移除 rspec gem 2. 如果您在切换到 minitest 时仍想运行 rspecs,您可以不理会“test”环境,并专门为 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/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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-04
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多