Git Product home page Git Product logo

whenever-test's Introduction

Whenever::Test

Gem Downloads Build Status GitHub Issues GitHub License

A gem that adds test support to Whenever gem.

Background

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:

group :test do
  gem 'whenever-test'
end

Usage

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:

  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.

Contributors

Contributing

  1. Fork it ( https://github.com/rafaelsales/whenever-test/fork )
  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

whenever-test's People

Contributors

apostergiou avatar franzejr avatar mike-stewart avatar rafaelsales avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.