Skip to content

Instantly share code, notes, and snippets.

@supremebeing7
Created September 10, 2014 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supremebeing7/2a479981588fea696d41 to your computer and use it in GitHub Desktop.
Save supremebeing7/2a479981588fea696d41 to your computer and use it in GitHub Desktop.
Rake task testing with Rspec
namespace :report do
task :daily => :environment do
report_for_all_users('daily_report', 'day')
end
def report_for_all_users(flag, time_period)
puts "Getting users who want #{flag.to_s.humanize}s"
users = User.where("admin = true AND settings -> '#{flag}' = '1'")
puts "Sending #{flag.to_s.humanize}s for #{users.count} users"
users.each do |user|
puts "Sending report for #{user.name}"
Report.generate_and_send(user, time_period)
end
puts "#{flag.to_s.humanize}s sent!"
end
end
require 'rspec'
require 'spec_helper'
require 'rake'
describe 'report namespace rake task' do
describe 'report:daily' do
let!(:account) { Fabricate(:account) }
let!(:user1) { Fabricate(:user, account: account, admin: true) }
let!(:user2) { Fabricate(:user, account: account, admin: true) }
let!(:user3) { Fabricate(:user, account: account, admin: false) }
before do
load "tasks/report.rake"
Rake::Task.define_task(:environment)
user1.update(settings:
{'daily_report' => '1',
'weekly_report' => '1',
'monthly_report' => '1'})
user2.update(settings:
{'daily_report' => '1',
'weekly_report' => '1',
'monthly_report' => '1'})
user3.update(settings:
{'daily_report' => '1',
'weekly_report' => '1',
'monthly_report' => '1'})
end
it "should call the generate_and_send method" do
Report.should_receive(:generate_and_send).with(user1, 'day')
Report.should_receive(:generate_and_send).with(user2, 'day')
Report.should_not_receive(:generate_and_send).with(user3, 'day')
Rake::Task["report:daily"].invoke
puts '{During Rspec run, no reports actually sent}'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment