public
Last active

Testing rake tasks

  • Download Gist
bar.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
bar_problematic.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
bar_rake_problematic_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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
bar_rake_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.