Rake Default namespace runs tests - ruby-on-rails

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.

Related

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

Determining whether gem was called as Rake task

I'm writing a gem that includes some rake tasks. I have some code in the before_configuration method that I want to run when my gem is loaded by the app at runtime, but NOT when a task is run with rake. How can I determine that?
lib/mygem/tasks.rake:
namespace :mygem do
task :dosomething do
puts "DONE"
end
end
lib/mygem/railtie.rb:
require "rails"
module Mygem
class Railtie < ::Rails::Railtie
config.before_configuration do
#is_rake_task = ?
if !is_rake_task
# Do something
end
end
end
end
Rake is only defined in rake context.
So, you could simply go something like that :
if defined? Rake
# rake specific stuff
else
# non rake stuff
end
Edit :
While this will work perfectly with rails s, there's a problem with zeus on development environment : zeus will require rake.
If this is a problem, if think you can take advantage of Rake.application, which sets an instance variable when a rake task is executed.
I've tested this in a zeus s context :
> Rake.instance_variable_defined? :#application
false
And in a rake <task> context :
> Rake.instance_variable_defined? :#application
true
So, a complete solution would be :
if defined?( Rake ) and Rake.instance_variable_defined?( :#application )
# rake specific stuff
else
# non rake stuff
end

run rspec while running test/unit tests

In our current applications we only run Test::Unit tests. So we do rake test and it runs all the tests under test.
I'm adding a rspec directory as well and I want the rspecs to run when I do rake test instead of manually also doing rspec spec.
Any idea how to combine both under rake test?
We have our TestTask under the gems for example vendor/gems/shoulda-2.10.3/Rakefile:Rake::TestTask.new do |t| ...
and in application.rb we have
config.generators do |g|
g.test_framework :shoulda
g.fallbacks[:shoulda] = :test_unit
g.fixture_replacement :factory_girl
end
Add the following line in the Rakefile, It will Run the spec and test task, when you execute the rake test
task :test => :spec
RSpec can actually run your Test::Unit cases for you, just require 'spec/test/unit'

rufus-scheduler fails with rake tasks (Don't know how to build task)

I'm using Rails3 (Windows, Ruby 1.8.7) with rufus-scheduler gem. Gem works fine, but if I'm trying to run some standard rake task, error occurs:
Don't know how to build task 'db:version' # ofc, db:version is just example
Terminal command
rake -T
works
If I'm trying to define own simple rake commands, they works fine too:
# /lib/my_scheduler.rb
require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( Rails.root, 'lib', 'tasks', 'my_own_tasks.rake')
scheduler = Rufus::Scheduler.start_new
scheduler.every '5s' do
Rake::Task["my_own_namespace:test"].invoke
end
end
# /lib/tasks/my_own_tasks.rb
namespace :my_own_namespace do
task :test do
puts "Some scheduler task"
end
end
... but using standard rake tasks *in my_own_tasks* throws the same error.
Some help would be appreciated
PS. I'm newbie, so sorry, if that was dumb question
Maybe someone will need solution:
system("rake namespace:task")
f.e:
system("rake db:version")

Rails Unit Test Issue with rake

I am working on writing tests for a rails 2.3.4 application and I am running into the following error when I try to run the tests
1) Failure:
default_test(ReportTest) [rake (0.8.7) lib/rake/rake_test_loader.rb:5]:
No tests were specified.
This is what the only test file looks like:
require File.dirname(__FILE__) + '/../test_helper'
class UserTest < ActiveSupport::TestCase
include Authlogic::TestCase
setup :activate_authlogic
fixtures :users
def setup
#user = users(:one)
end
def test_user_is_valid
assert #user.valid?
end
end
One issue I could forsee being a problem is that I have multiple versions of rails installed and rake as well
rails (3.0.0, 2.3.8, 2.3.5, 2.3.4, 1.2.6)
rake (0.8.7, 0.8.3)
Anyone know what's going on?
Somehow the search for tests that rake test does has turned up a file named _test.rb which doesn't have a test in it. It will look through all the files under the test/units /functionals and /integration directories to try and locate tests.
I also once had the strange behaviour that a rails application in a sub directory of another rails application was finding more tests than it should have done as it was picking up tests from the 'parent' application. Might be worth checking that too.

Resources