Last active
December 20, 2015 08:59
-
-
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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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