You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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, ...
Installation
Since it's available in Rubygems, just add the following to your Gemfile:
You can write a spec such as (RSpec was used in this example):
# spec/whenever_spec.rbrequire'spec_helper'describe'Whenever Schedule'dobeforedoload'Rakefile'# Makes sure rake tasks are loaded so you can assert in rake jobsendit'makes sure `runner` statements exist'doschedule=Whenever::Test::Schedule.new(file: 'config/schedule.rb')assert_equal2,schedule.jobs[:runner].count# Executes the actual ruby statement to make sure all constants and methods exist:schedule.jobs[:runner].each{ |job| instance_evaljob[:task]}endit'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:assertRake::Task.task_defined?(schedule.jobs[:rake].first[:task])endit'makes sure cron alive monitor is registered in minute basis'doschedule=Whenever::Test::Schedule.new(file: fixture)assert_equal'https://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]endend
Now the important part: these specs guarantee that:
If TimeoutOffers constant is not defined or TimeoutOffers.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
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.