First gem that allows testing whenever-gem jobs
Latest commit 43a6118 Dec 13, 2016 @rafaelsales committed on GitHub Merge pull request #4 from apostergiou/patch-1


Gem Downloads Build Status GitHub Issues GitHub License

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'


Suppose you have a schedule such as:

# config/schedule.rb
job_type :curl, 'curl :task'

every :hour { runner 'TimeoutOffers.perform_async' }
every :minute { curl '' }

if @environment == 'staging'
  every :day { rake 'db_sync:import' }

You can write a spec such as (Minitest 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

  it 'makes sure `runner` statements exist' do
    schedule = 'config/schedule.rb')

    assert_equal 2,[:runner].count

    # Executes the actual ruby statement to make sure all constants and methods exist:[:runner].each { |job| instance_eval job[:task] }

  it 'makes sure `rake` statements exist' do
    # config/schedule.rb file is used by default in constructor:
    schedule = { environment: 'staging' })

    # Makes sure the rake task is defined:
    assert Rake::Task.task_defined?([:rake].first[:task])

  it 'makes sure cron alive monitor is registered in minute basis' do
    schedule = fixture)

    assert_equal '',[:curl].first[:task]
    assert_equal 'curl :task',[:curl].first[:command]
    assert_equal [:minute],[:curl].first[:every]

Now the important part: these specs guarantee that:

  1. If TimeoutOffers constant is not defined or TimeoutOffers.perform_async method doesn't exist, the spec fails
  2. If Rake task db_sync:import doesn't exist, the spec fails
  3. You should have a custom task named curl to make sure that job will work, otherwise the spec fails

How it works

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.



  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request