Created
June 13, 2012 18:20
-
-
Save xoebus/2925631 to your computer and use it in GitHub Desktop.
Pester Proxy
This file contains hidden or 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
class PesterProxy | |
def initialize(target, methods={}) | |
@__pester_target = target | |
@__pester_limit = methods | |
@__pester_count = Hash.new | |
methods.keys.each { |key| @__pester_count[key] = 0 } | |
end | |
def method_missing(sym, *args, &block) | |
count = @__pester_count.fetch(sym) do | |
@__pester_target.call(sym, args, block) | |
return | |
end | |
limit = @__pester_limit.fetch(sym) | |
if count == 0 | |
@__pester_target.call(sym, args, block) | |
end | |
if count == limit-1 | |
@__pester_count[sym] = 0 | |
else | |
@__pester_count[sym] += 1 | |
end | |
end | |
end |
This file contains hidden or 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
require 'rspec' | |
require_relative 'pester' | |
describe PesterProxy do | |
describe 'call limiting' do | |
before :each do | |
@target = mock('target') | |
@proxy = PesterProxy.new(@target, :hello => 3) | |
end | |
it 'stops multiple calls executing' do | |
@target.should_receive(:call).with(:hello, [], nil).once | |
3.times { @proxy.hello } | |
end | |
it 'trys again after enough tries' do | |
@target.should_receive(:call).with(:hello, [], nil).twice | |
4.times { @proxy.hello } | |
end | |
it 'does not affect the unnamed methods' do | |
@target.should_receive(:call).with(:bye, [], nil) | |
@proxy.bye | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment