我想从模型开始,将现有的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测试和断言都消失了。可能的原因是什么?
系统信息:
x86_64-darwin10.8.0
发布于 2011-08-08 22:23:20
由于您正在从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 -将配置/环境/test.rb复制到配置/环境/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"https://stackoverflow.com/questions/6803291
复制相似问题