Created
May 23, 2012 16:48
-
-
Save maxjustus/2776305 to your computer and use it in GitHub Desktop.
Recursive stubs with method expectations for things like arel query chains
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 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