-
-
Save shipstar/1981349 to your computer and use it in GitHub Desktop.
Toy stub framework with Ruby
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
# Playing around with a Toy Stubbing "framework" | |
class Statistics | |
def initialize(nums) | |
@nums = nums | |
end | |
def compute_average | |
sum = 0; | |
@nums.each do |num| | |
sum += num | |
end | |
sum.to_f / @nums.length | |
end | |
end | |
module Stub | |
def self.extended(base) | |
klass = base.class | |
methods_to_stub = klass.instance_methods - Object.instance_methods | |
klass.instance_eval do | |
methods_to_stub.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ } | |
end | |
base.instance_eval{ @canned_responses = {} } | |
end | |
def method_missing(m, *args, &block) | |
if @canned_responses.has_key?(m) | |
@canned_responses[m] | |
else | |
raise "Unstubbed method!" | |
end | |
end | |
def canned(m, ret) | |
@canned_responses[m] = ret | |
end | |
end | |
s = Statistics.new(nil) | |
s.extend Stub | |
s.canned(:compute_average, 6.5) | |
p "The average is: #{s.compute_average}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Upon further reflection" - I see what you did there ;-P