Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created May 23, 2012 16:48
Show Gist options
  • Save maxjustus/2776305 to your computer and use it in GitHub Desktop.
Save maxjustus/2776305 to your computer and use it in GitHub Desktop.
Recursive stubs with method expectations for things like arel query chains
class ChainStub
attr_accessor :instance, :result
def initialize(instance)
@instance = instance
@result = Object.new
end
def method_missing(method_name, *args, &blk)
if blk
instance.stub(method_name).with(*args) { blk.call }
nil
else
instance.stub(method_name).with(*args) { result }
self.class.new(result)
end
end
end
def ChainStub(instance)
ChainStub.new(instance)
end
instance = Object.new
result = [Object.new]
ChainStub(instance).where(:herp => 'derp').scoped_to_store(1) { result }
instance.where(:herp => 'derp').scoped_to_store(1).should == result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment