Skip to content

Instantly share code, notes, and snippets.

@mikekelly
Forked from deepak/rspec_stub_chain_if_spec.rb
Created September 27, 2012 10:50
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 mikekelly/3793426 to your computer and use it in GitHub Desktop.
Save mikekelly/3793426 to your computer and use it in GitHub Desktop.
mocking stub chains with an assertion if the final call is made
require 'rspec'
class DevOps
attr_reader :monitor
def initialize(arguments)
@monitor = arguments.fetch(:monitor) { Monitor.new }
end
def check
monitor.ping('DS1')
end
def noop
end
end
class Monitor
def ping(name)
"pong #{name}"
end
end
describe DevOps do
let(:devops) { DevOps.new(monitor: mock_monitor) }
let(:mock_monitor) { mock }
it "#monitor called", :wip => true do
mock_monitor.should_receive(:ping).with('DS1').and_return(stub.as_null_object)
devops.check
end
it "#monitor not called" do
devops.noop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment