Skip to content

Instantly share code, notes, and snippets.

@shageman
Created February 8, 2012 02:08
Show Gist options
  • Save shageman/1764423 to your computer and use it in GitHub Desktop.
Save shageman/1764423 to your computer and use it in GitHub Desktop.
Testing rake tasks
File: lib/tasks/bar.rake
class BarOutput
def self.banner text
puts '*' * 60
puts " #{text}"
puts '*' * 60
end
def self.puts string
puts string
end
end
namespace :foo do
desc "bake some bars"
task bake_a_bar: :environment do
BarOutput.banner " Step back: baking in action!"
BarOutput.puts Bar.new.bake
BarOutput.banner " All done. Thank you for your patience."
end
end
File: lib/tasks/bar_problematic.rake
namespace :foo do
desc "bake some bars"
task bake_a_problematic_bar: :environment do
puts '*' * 60
puts ' Step back: baking in action!'
puts '*' * 60
puts Bar.new.bake
puts '*' * 60
puts ' All done. Thank you for your patience.'
puts '*' * 60
end
end
File: spec/tasks/bar_rake_problematic_spec.rb
require 'spec_helper'
require 'rake'
describe 'foo namespace rake task' do
describe 'foo:bake_a_problematic_bar' do
before do
load File.expand_path("../../../lib/tasks/bar_problematic.rake", __FILE__)
Rake::Task.define_task(:environment)
end
it "should bake a bar" do
Bar.any_instance.should_receive :bake
Rake::Task["foo:bake_a_problematic_bar"].invoke
end
it "should bake a bar again" do
Bar.any_instance.should_receive :bake
Rake::Task["foo:bake_a_problematic_bar"].invoke
end
end
end
File: spec/tasks/bar_rake_spec.rb
require 'spec_helper'
require 'rake'
describe 'foo namespace rake task' do
before :all do
Rake.application.rake_require "tasks/bar"
Rake::Task.define_task(:environment)
end
describe 'foo:bar' do
before do
BarOutput.stub(:banner)
BarOutput.stub(:puts)
end
let :run_rake_task do
Rake::Task["foo:bake_a_bar"].reenable
Rake.application.invoke_task "foo:bake_a_bar"
end
it "should bake a bar" do
Bar.any_instance.should_receive :bake
run_rake_task
end
it "should bake a bar again" do
Bar.any_instance.should_receive :bake
run_rake_task
end
it "should output two banners" do
BarOutput.should_receive(:banner).twice
run_rake_task
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment