Skip to content

Instantly share code, notes, and snippets.

@readysetawesome
Last active December 14, 2015 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save readysetawesome/5062199 to your computer and use it in GitHub Desktop.
Save readysetawesome/5062199 to your computer and use it in GitHub Desktop.
This is great for testing chained scopes in rails, which I think really should be this simple.
class ChainedMock < RSpec::Mocks::Mock
class WrongMethodCalled < StandardError; end
def method_missing meth, *args, &block
raise WrongMethodCalled.new "expected method #{@next} to be next in the chain, but instead received #{meth}"
end
def should_receive(meth, opts = {}, &block)
@next = meth
super
end
end
class Object
def should_receive_chain *args
if args.size == 1
should_receive(args[0], "missing call to method #{args[0]} in chained expectation")
else
exp = ChainedMock.new(:chained_expectation)
top = args.slice!(0)
should_receive(top, "missing call to method #{top} in chained expectation").and_return(exp)
if self.class != ChainedMock
args.each_with_index do |arg, idx|
stub(arg) do
raise ChainedMock::WrongMethodCalled.new "expected method #{top} to be first in the chain, but instead received #{arg}"
end
end
end
exp.should_receive_chain *args
end
end
end
# usage
User.should_receive_chain(:registered, :logged_in_once).and_return([user])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment