Skip to content

Instantly share code, notes, and snippets.

@masarakki
Created September 28, 2010 06:57
Show Gist options
  • Save masarakki/600538 to your computer and use it in GitHub Desktop.
Save masarakki/600538 to your computer and use it in GitHub Desktop.
#
# extension for rspec
# this extension enables to check should_receive in stub_chain
# you must require this file in 'spec/spec_helper.rb'
#
#
# Example:
# in controller,
#
# def index
# @comments = @article.comments.find(:all, :conditions => {:disable => false})
# end
#
# you can test should_receive like this
#
# describe "GET index" do
# @article.stub_and_should_receive(:comments, :find).with(:all, :conditions => {:disable => false}).and_return([mock_comment])
# get :index
# end
#
#
#
module SpecExt
module Mock
module Methods
def stub_and_should_receive(*methods, &block)
if methods.length > 2 || methods.length > 1 && !methods.last.is_a?(Hash)
next_in_chain = Object.new
stub!(methods.shift) {next_in_chain}
next_in_chain.stub_and_should_receive(*methods, &block)
else
should_receive(methods.shift, methods.last || {}, &block)
end
end
def stub_and_should_not_receive(sym, &block)
if methods.length > 1
next_in_chain = Object.new
stub!(methods.shift) {next_in_chain}
next_in_chain.stub_and_should_receive(*methods, &block)
else
should_not_receive(methods.shift, &block)
end
end
end
end
class Object
include SpecExt::Mock::Methods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment