Skip to content

Instantly share code, notes, and snippets.

@xoebus
Created June 13, 2012 18:20
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 xoebus/2925631 to your computer and use it in GitHub Desktop.
Save xoebus/2925631 to your computer and use it in GitHub Desktop.
Pester Proxy
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
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