A gem that adds test support to Whenever gem.
I've been part of many projects that used Whenever, but testing the schedule.rb
file was always neglected.
Turns out your jobs defined in Whenever schedule might reference rake tasks that don't exist in runtime and not even have correct ruby syntax (in case or runner-type jobs).
This gem adds a support class so you can write specs that assert against definitions in the schedule.rb
file. To make sure ruby statements referenced in runner-type jobs actually work, you can instance_eval
them and write expectations on what should happen, and then you'll be sure cron jobs won't have runtime issues that are detected only in staging or production environments.
NOTE: This gem is test-framework agnostic, so you can use with RSpec, MiniTest, ...
Since it's available in Rubygems, just add the following to your Gemfile:
group :test do
gem 'whenever-test'
end
Suppose you have a schedule such as:
# config/schedule.rb
job_type :curl, 'curl :task'
every :hour { runner 'TimeoutOffers.perform_async' }
every :minute { curl 'http://myapp.com/cron-alive' }
if @environment == 'staging'
every :day { rake 'db_sync:import' }
end
You can write a spec such as (RSpec was used in this example):
# spec/whenever_spec.rb
require 'spec_helper'
describe 'Whenever Schedule' do
before do
load 'Rakefile' # Makes sure rake tasks are loaded so you can assert in rake jobs
end
it 'makes sure `runner` statements exist' do
schedule = Whenever::Test::Schedule.new(file: 'config/schedule.rb')
assert_equal 2, schedule.jobs[:runner].count
# Executes the actual ruby statement to make sure all constants and methods exist:
schedule.jobs[:runner].each { |job| instance_eval job[:task] }
end
it 'makes sure `rake` statements exist' do
# config/schedule.rb file is used by default in constructor:
schedule = Whenever::Test::Schedule.new(vars: { environment: 'staging' })
# Makes sure the rake task is defined:
assert Rake::Task.task_defined?(schedule.jobs[:rake].first[:task])
end
it 'makes sure cron alive monitor is registered in minute basis' do
schedule = Whenever::Test::Schedule.new(file: fixture)
assert_equal 'http://myapp.com/cron-alive', schedule.jobs[:curl].first[:task]
assert_equal 'curl :task', schedule.jobs[:curl].first[:command]
assert_equal [:minute], schedule.jobs[:curl].first[:every]
end
end
Now the important part: these specs guarantee that:
- If
TimeoutOffers
constant is not defined orTimeoutOffers.perform_async
method doesn't exist, the spec fails - If Rake task
db_sync:import
doesn't exist, the spec fails - You should have a custom task named
curl
to make sure that job will work, otherwise the spec fails
This gem implements a class that has the same DSL interface as Whenever gem. It basically runs the schedule.rb
file against Whenever::Test::DSLInterpreter
and stores all statements and parameters found so you can easily query them in your tests.
- Rafael Sales @rafaelsales
- Mike Stewart @mike-stewart
- Fork it ( https://github.com/rafaelsales/whenever-test/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request