I’m working on the RubyLearning blog’s Ruby Programming Challenge for Newbies #1 to learn the language – I’ve done a bit with Rails and some admin scripts so I could use the exposure.  Since I love TDD approaching Ruby development through RSpec is only natural, but it was a pain in the ass trying to find how I could have RSpec verify that my program properly exited when certain conditions were met.  Here’s how I solved it, reposted here:

# something.rb 
class Something
def initialize(kernel=Kernel) @kernel = kernel end

def process_arguments(args)



require 'something'
describe Something do
before :each do @mockkernel = mock(Kernel) @mockkernel.stub!(:exit) end

it "should exit cleanly" do
    s = Something.new(@mock_kernel)


What I learned was that you can define a constructor with optional arguments (in this case, initialize(kernel=Kernel) and then proceed to use @kernel’s methods instead of the methods that Kernel provides when you do not specify a class instance.  With a properly mocked and stubbed exit method in my spec things operate as expected.