On Rails 6 (6.1.4.1) we had a RakeFile that would run a subset of tests. For example:
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.run(test_files)
end
end
This would execute just fine when called:
rails test:carriers
However, somewhere along the way, something changed and we began seeing errors when trying to run our RakeFile test tasks. (I haven't tracked down exactly what changed and when it changed -- perhaps it was part of the Rails 7 release.) Here's the error we began seeing:
rails aborted!
NameError: uninitialized constant Shoulda
Shoulda::Matchers.configure do |config|
^^^^^^^
/path/test/test_helper.rb:15:in `<main>'
/path/test/models/carrier_test.rb:1:in `<main>'
/path/lib/tasks/carriers.rake:11:in `block (2 levels) in <main>'
Tasks: TOP => test:carriers
(See full trace by running task with --trace)
The error appeared with no changes to our tests or environment configuration. (Running a full rake worked just fine.)
When reviewing the source code for Rails::TestUnit::Runner, I came across rake_run. Simply replacing Rails::TestUnit::Runner.run with Rails::TestUnit::Runner.rake_run addressed the issue (no other changes required):
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.rake_run(test_files)
end
end
Related
I am trying to upgrade a Rails 6.1.3.1 application from Ruby 2.6.6 to 3.0.0
All the rspec tests run fine and the in development everything seems to work just fine except one thing:
Even the simplest of tasks fails with this error:
"class variable ##debug_missing_translation of ActionView::Base is overtaken by Object"
For example, this simpletask.rake file
task simpletask: :environment do
puts 'Hello'
end
is not able to run as I get the following error:
lxxx#xxx:~/Workspace/edumino$ rails simpletask
rails aborted!
class variable ##debug_missing_translation of ActionView::Base is overtaken by Object
/home/xxx/railsapp/config/environment.rb:5:in `<top (required)>'
/home/xxx/railsapp/bin/rails:5:in `require'
/home/xxx/railsapp/bin/rails:5:in `<top (required)>'
/home/xxx/railsapp/bin/spring:10:in `require'
/home/xxx/railsapp/bin/spring:10:in `block in <top (required)>'
/home/xxx/railsapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => simpletask => environment
(See full trace by running task with --trace)
...so, it turns out the problem was an already existing task. I don't remember exactly why this was necessary but the task had this:
# frozen_string_literal: true
require "#{Rails.root}/app/helpers/cron_helper"
require "#{Rails.root}/app/helpers/notifier_helper"
require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper
include ActionView::Helpers::TranslationHelper
include CronHelper
namespace :cron do
task sms: :environment do |_t, _args|
...
end
end
I assume that the guilty code was including the ActionView::Helpers::TranslationHelper
Again, I can't remember why I had to do that at that time. But if you bump into this kind of 'overtaken by' error when upgrading to Ruby 3.0, check your other tasks and see what kinda weird includes you might have had. In my case, since I didn't need the old task, I just removed it.
The following command is supposed to create a new database.
rails db:create
Where is this function defined? Or is this a pre-packaged function in rails?
It's in the databases.rake file of the framework:
namespace :create do
task all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
desc "Create #{spec_name} database for current environment"
task spec_name => :load_config do
db_config = ActiveRecord::DatabaseConfigurations.config_for_env_and_spec(Rails.env, spec_name)
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
end
end
end
Whenever you doubt or want to know where a task has been defined, you can use the rails -W (or rake) command, passing the task:
$ rails -W db:create
rails db:create /path/databases.rake:26:in `block in <top (required)>'
rails db:create:all /path/databases.rake:20:in `block (2 levels) in <top (required)>'
Note it was introduced in the version 0.9 of Rake. This might or might not work depending on the versions which you're working with.
I have an Ruby 2.1/Rails 3.2 application which uses the asset pipeline. We are also using a fragile (alpha) gem which causes "rake assets:precompile" to fail at times. I would like to write an rspec test which ensures that this rake task always passes before we commit our code.
I wrote a test in spec/asset_precompile_spec.rb which looks like this:
require 'spec_helper'
require 'rake'
describe 'assets:precompile' do
before { MyApp::Application.load_tasks }
it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception }
end
I then ran it on the command line using
rspec spec/lib/assets_precompile_spec.rb
The output I got looked like this:
1) assets:precompile
Failure/Error: it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception }
expected no Exception, got #<RuntimeError: Command failed with status (1): [/home/railsdev/.rvm/rubies/ruby-2.1.2/bin/...]> with backtrace:
# ./spec/lib/assets_precompile_spec.rb:7:in `block (3 levels) in <top (required)>'
# ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.71247 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/lib/assets_precompile_spec.rb:7 # assets:precompile
I have looked far & wide, and I can't find any example to run "rake assets:precompile" which actually works in my RSpec environment. I have tried explicitly loading the spec_helper.rb file, I have tried explicitly requiring "factory_girl", but I cannot find anything which works.
Is there a way to make a test run this rake task run in a RSpec test?
Try Rake::Task['assets:precompile:all'].invoke
instead of Rake::Task['assets:precompile'].invoke
In my case it helped.
I have this rake task in a Rails 3.2.11 application in lib/rake/searches.rb:
namespace :searches do
desc "Start background searches"
task :start => :environment do
Rails.logger.info "Starting searches..."
Campaign.all.each do |c|
next unless c.recurring?
Rails.logger.info "Starting searches for campaign '#{c.name}'"
SearchWorker.enqueue(:campaign_id => c.id, :clear => true)
end
end
end
When I run it locally everything goes well. When I run it in production it errors out:
$ bundle exec rake searches:start
rake aborted!
uninitialized constant SearchWorker
/var/apps/web/lib/tasks/searches.rake:9:in `block (3 levels) in <top (required)>'
/var/apps/web/lib/tasks/searches.rake:5:in `block (2 levels) in <top (required)>'
Tasks: TOP => searches:start
(See full trace by running task with --trace)
When I jump into a console session, I can see that the class is correctly auto loaded:
$ bundle exec rails console
Loading production environment (Rails 3.2.11)
irb(main):001:0> SearchWorker
=> SearchWorker
This workers live in app/workers and they are added to the autoload_paths config setting in application.rb:
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(
#{config.root}/custom_scripts
#{config.root}/app/workers
#{config.root}/app/models/filters
)
So I have no clue why the error only occurs in production, and when running from a rake task.
Any ideas?
It seems you have created the rake file with .rb extension
lib/rake/searches.rb
can you try changing it to
lib/rake/searches.rake
That should work
This is due to the production environment in Rails 3.2 not being fully loaded, after the :environment task is complete.
If you explicitly require SearchWorker ie.
require 'app/workers/search_worker'
Within the beginning of the task block, it'll work.
(Why auto-loads isn't doing this for you, I don't know)
I'm trying to create an xml runner to make a result report of Rails unit testing. Here is a code I have:
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class FastFailRunner < Test::Unit::UI::Console::TestRunner
def add_fault(fault)
#faults << fault
nl
output("%3d) %s" % [#faults.length, fault.long_display])
output("--")
#already_outputted = true
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
nl
output(#result)
end
end
Test::Unit::AutoRunner::RUNNERS[:fastfail] = proc do |r|
FastFailRunner
end
When I run it as TESTOPTS="/home/alex/RubymineProjects/app2/test/unit/runner.rb --runner=xml" rake test
... I get an error (pretty weird error)
/home/alex/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
gem install minitest
/home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
Errors running test:units! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
I tried to require test/unit/ui/xml/testrunner.rb but no luck.
Any thoughts?
If you are in Ruby 1.8.7 you can say:
require 'test/unit/ui/console/testrunner'
p Test::Unit::UI::Console::TestRunner # => no problem
(There is no such file as test/unit/ui/xml/testrunner.rb so I'm not sure what you were up to there.)
Look in the docs in test/unit.rb, there's actually sample code showing you how to do this require: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html.
However, the problem is that you are in Ruby 1.9.3. There is no /test/unit in Ruby 1.9.3! Well, there is, but it's just a compatibility layer for basic tests; there is certainly no test/unit/ui/console/testrunner, and no module/class Test::Unit::UI::Console::TestRunner.
Instead, there's minitest. You can read the docs on minitest to see how to make a test runner. http://docs.seattlerb.org/minitest/
One thing to consider is that Test::Unit was included in the default Ruby 1.8.7 installation. If you want to use it in with a later version of Ruby then go ahead and install it as a gem.
sudo gem install test-unit
http://test-unit.rubyforge.org/