Skip to content

Instantly share code, notes, and snippets.

@pburkholder
Last active December 20, 2015 08:59
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 pburkholder/6104189 to your computer and use it in GitHub Desktop.
Save pburkholder/6104189 to your computer and use it in GitHub Desktop.
This is a good example for my own reference demonstrating a few things with MixIns, Rspec, and OptionParse * Class Real gets the methods .hallo and .parse_options by dint of a MixIn (with include) * The spec context 'with include' show that the hallo method works w/o needing to be instantiated * The spec context 'with Real' shows how I can stub …
module MyModule
module Utils
def hallo(options)
if ( options[:verbose] )
'hallo'
else
'buzz_off'
end
end
def parse_options()
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
end.parse!
options
end
end
class Real
include Utils
end
end
describe MyModule do
context "with include" do
include MyModule::Utils
it { hallo({}).should == "buzz_off" }
end
context "with Real" do
it "should work" do
obj = MyModule::Real.new()
obj.stub(:parse_options).and_return({:verbose=>true})
opts = obj.parse_options()
obj.hallo(opts).should == 'hallo'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment