run rspec while running test/unit tests - ruby-on-rails

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'

Related

Require a gem in all migrations

I have this gem in my gemfile (gem "lhm", "~> 2.2.0", require: false) that I want required in all migrations. Behavior similar to that of requiring spec_helper in all spec files.
I considered doing something with bin/rails or bin/rake but I don't want it required in all tasks, just migrations. Ie. rake db:migrate or the now alias rails db:migrate
As you probably don't have all the migrations using lhm I would suggest adding require "lhm" only to the migrations you're using. But to answer your question you could use rake's enhance method:
add the below to lib/tasks/something.rake:
namespace :load do
namespace :lhm do
desc "This just loads lhm"
task lhm: :environment do
require "lhm"
end
end
end
Rake::Task['db:migrate'].enhance(['load:lhm'])
As an example see here

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.

Create my own rake test:foo task

I have a bunch of tests that aren't unit or functional tests, they're of the format test/foo/special_test.rb
I want to create a rake task like rake test:units that will run all the tests in the foo folder. How do I do this?
Edit: I'd actually like rake test:foo to be a little different from rake test:units, in that I do not want it to run when I do simply rake test.
I don't remember where this is from, so unfortunately I can't give proper acknowledgement, but this should work. I say "should" because I've stopped using it, but grabbed it from my git history.
# First, 'reopen' the default :test namespace and create your custom task.
namespace :test do
Rake::TestTask.new(:foo_tests => ["test:prepare", "other_dependent_rake_tasks"] ) do |t|
DatabaseCleaner.strategy = :transaction # If using this.
t.libs << "test"
# Will also get subfolders within test/foo
t.test_files = FileList['test/foo/**/*_test.rb', 'test/foo/*_test.rb']
end
end
You can remove the default "test" task and redefine it so that when you run rake test it will automatically also run rake test:foo_tests.
remove_task "test"
desc 'Adding onto Rails regular tests'
task :test do
# Add all the names of tests you want run here.
errors = %w(test:units test:functionals test:integration test:foo_tests).collect do |task|
begin
puts "Running: #{task}"
Rake::Task[task].invoke
nil
rescue => e
task
end
end.compact
abort "Errors running #{errors * ', '}!" if errors.any?
end

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 testing with Watir. How do I load my test db with the fixtures from /test/fixtures

I'm trying to test my Rails App with test fixtures using Watir. The watir tests work but I can't seem to load my test db with the fixtures from /test/fixtures.
I tried using rake tasks with RAILS_ENV=test but only managed to load the fixtures from /db/fixtures/
Found a way to do this using rake tasks.
namespace :watir do
desc "Initiate the db for watir tests"
task :db_reset do
if (ENV['RAILS_ENV'] == "test")
Rake::Task["db:test:prepare"].invoke
Rake::Task["db:test:load"].invoke
Rake::Task["db:seed"].invoke
Rake::Task["db:fixtures:load"].invoke("FIXTURES=x,y,z,....") #x,y,z are the name of the fixtures in test/fixtures/*
else
puts "Must be executed with test flag ('RAILS_ENV=test')"
end
end
end
To run it I do rake watir:db_reset
Now I can run the watir tests on the test fixtures

Resources