16

モデルから始めて、既存のRailsアプリケーションをrspecからminitestに切り替えたいと思います。そのため、フォルダを作成しましたtestminitest_helper.rbその中に、次の内容の名前のファイルを作成しました。

require "minitest/autorun"

ENV["RAILS_ENV"] = "test"

modelsおよびを含むフォルダ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

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

テストとアサーションはなくなりました。その理由は何でしょうか?

システム情報:

  • ルビー1.9.2p180(2011-02-18リビジョン30909)[x86_64-darwin10.8.0]
  • Rails 3.1.0.rc4
4

1 に答える 1

16

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"
于 2011-08-08T14:23:20.320 に答える