Rails and MiniTest: add additional folder - ruby-on-rails

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.
Where do I have to add the lib folder?
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.

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
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 }
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 }

Related

How do you include tests from a lib directory in Rails rake tasks

How do you include tests that are in a lib folder in your rake tasks?
For example, you have a foo library you are building in the /lib/foo directory of your Rails project and would like to keep all of your foo tests in the lib/foo/tests directory.
It took me a while to consolidate from all the different sources, so I wanted to post here for anyone looking!
This imports the rake file from my lib/foo/test directory:
# Rakefile:
Dir.glob('lib/foo/tasks/*.rake').each { |r| load r}
This adds the test:foo_tests task to my rake tasks:
# lib/foo/tasks/test.rake
require "rake/testtask"
namespace :test do
Rake::TestTask.new(foo_tests: 'test:prepare') do |t|
t.pattern = 'lib/foo/test/**/*_test.rb'
end
end
I hope this helps someone else!

Run tests in custom test folder in Rails 5

When I want to run all model tests we do
rails test:models
If I similarly would like to run tests that sits in a folder called service_objects (in the test folder) - what would be the required steps?
With inspiration from multiple, sources I have tried the following in lib/tasks:
namespace :test do
Rails::TestTask.new(classes: 'test:prepare') do |t|
t.pattern = 'test/service_objects/**/*_test.rb'
end
end
But running rails test:service_objects returns this error message:
NameError: uninitialized constant Rails::TestTask
Replace Rails::TestTask with Rails::TestUnit::Runner as shown in the file below, with the require path indicated at the top.
Then to run ONLY the tests in test/focused directory, you can call
rails test:focused
and to run ALL tests in the test directory EXCEPT those in test/long_running, you can call
rails test:without_long_running
Tested with Rails 5.1.6
The new Rails 5 test runner does have some really helpful features, but sometimes you still need a little more control.
# lib/tasks/test_tasks.rake
require "rails/test_unit/runner"
namespace :test do
task :focused => "test:prepare" do
$: << "test"
test_files = FileList['test/focused/*_test.rb']
Rails::TestUnit::Runner.run(test_files)
end
task :without_long_running_tests => "test:prepare" do
$: << "test"
test_files = FileList['test/**/*_test.rb'].exclude('test/long_running/**/*_test.rb')
Rails::TestUnit::Runner.run(test_files)
end
end
Credit should go to jonatack may 2015 post here: https://github.com/rails/rails/issues/19997

Rake Default namespace runs tests

I'm attempting to run some tests on my rails application, and they're working, which is great. However, I'm noticing that when I just run rake it defaults to running my tests. If anybody has run into this before and can shed some light on why this is happening, I'd appreciate it.
I'm using
rails 4.1.0
ruby 2.0.0
factory girl rails
minitest rails
minitest rails capybara
database cleaner
Rakefile
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
The default rake task is defined in rails/railties/Rakefile, and it runs all unit tests by default.

How do I get file name of the file currently being tested by rake

In my rake task if I want to know the name of the file when that file is picked up for testing then how do I do that. Reason is that some of the files produce warning. I am not sure which of my 800 tests is producing warning.
My rake task is something like this. I am using rails3.
Rake::TestTask.new(:test_hr_module) do |t|
t.libs << 'test'
t.test_files = Dir.glob('test/{hr}/**/*_test.rb').sort
t.warning = true
t.verbose = true
end
You can always call single files:
ruby test/unit/model_test.rb
You can even use the name flag to only run specific tests
ruby test/unit/model_test.rb -n test_name
You'll probably need to change require 'test_helper' to require 'test/test_helper' in the test file though.
If you really need to do this in a rake task could you try:
Dir.glob('test/**/*_test.rb').each do |f|
puts `ruby #{f}`
end
end
Perhaps you can use the pseudo-variable __FILE__ to achieve this?

Using Rake on a gem with dependencies

I have a gem that requires a 'Cms' namespace to be present when running.
However, when running rake tasks, nothing works as this Cms namespace isn't present. How do I get my rake tasks to work?
You can either, load your project source into the Rakefile (like Rails would do) or define a dummy module with the name Cms on your project.
# Rakefile
module Cms; end
task :my_task do
# ..
end
If you are on rails, and this gem is a dependency, you just have to make your task dependent of the :environment rails' task.
# some_task.rake
task :my_task => :environment do
# ..
end
Hope this helps.

Resources