问题描述
我使用Ruby 2和Rails4.我有一个文件夹test/lib
,其中包含一些测试.但是运行rake test
不会使用它们.仅其他测试(模型,控制器等)正在运行.
I use Ruby 2 and Rails 4. I have a folder test/lib
, where a few tests are located.But running rake test
does not use them. Only the other tests (models, controllers, ...) are running.
我必须在哪里添加lib
文件夹?
Where do I have to add the lib
folder?
我已经尝试过MiniTest::Rails::Testing.default_tasks << 'lib'
,但是得到NameError Exception: uninitialized constant MiniTest::Rails
.我没有将最小的gem添加到我的Gemfile中,因为Ruby 2默认使用它.
I already tried MiniTest::Rails::Testing.default_tasks << 'lib'
, but I get NameError Exception: uninitialized constant MiniTest::Rails
. I did not add the minitest gem to my Gemfile, because Ruby 2 uses it by default.
推荐答案
要使用MiniTest::Rails::Testing.default_tasks << 'lib'
,您需要添加 minitest-rails 宝石添加到您的Gemfile中.它与Minitest分开,并添加了许多Minitest功能,这些功能默认情况下未在Rails中启用. minitest-rails添加了其他功能,例如为所有具有测试的目录创建rake任务.因此,无需对Rakefile进行任何更改,您就可以运行以下代码:
To use MiniTest::Rails::Testing.default_tasks << 'lib'
you need to add the minitest-rails gem to your Gemfile. It is separate from Minitest and adds enables many Minitest features missing that are not enabled in Rails by default. And minitest-rails adds other features, such as creating rake tasks for all the directories that have tests. So without any changes to your Rakefile you can run things like this:
$ rake minitest:lib
或者,以老式的方式执行此操作,您可以将以下内容添加到Rakefile中:
Alternatively, to do this the old fashioned way, you can add the following to your Rakefile:
namespace :test do
desc "Test lib source"
Rake::TestTask.new(:lib) do |t|
t.libs << "test"
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = true
end
end
Rake::Task[:test].enhance { Rake::Task["test:lib"].invoke }
这假定您要在不使用任何数据库固定装置的情况下运行lib测试.如果需要固定装置和数据库事务,则应创建依赖于"test:prepare"的rake任务.
This assumes you want to run your lib tests without using any database fixtures. If you want the fixtures and database transactions, then you should create the rake task with a dependency on "test:prepare".
namespace :test do
desc "Test lib source"
Rake::TestTask.new(:lib => "test:prepare") do |t|
t.libs << "test"
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = true
end
end
Rake::Task[:test].enhance { Rake::Task["test:lib"].invoke }
这篇关于Rails和MiniTest:添加其他文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!