-
-
Save benlovell/3793442 to your computer and use it in GitHub Desktop.
mocking stub chains with an assertion if the final call is made
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
# mocking stub chains with an assertion if the final call is made | |
# check https://github.com/rspec/rspec-mocks/issues/133 and | |
# https://groups.google.com/d/topic/rspec/Dc98rP-3IFM/discussion | |
require 'rspec' | |
class DevOps | |
attr_reader :monitor | |
def initialize(monitor = Monitor.new) | |
@monitor = monitor | |
end | |
def check | |
monitor.ping('DS1') | |
end | |
def noop | |
end | |
end | |
class Monitor | |
def ping(name) | |
"pong #{name}" | |
end | |
end | |
describe DevOps do | |
let(:monitor) { mock(Monitor) } | |
let(:devops) { DevOps.new(monitor) } | |
it "#monitor called", :wip => true do | |
monitor.should_receive(:ping).with('DS1').and_return("pong DS1") | |
devops.check | |
end | |
it "#monitor not called" do | |
monitor.should_not_receive(:ping) | |
devops.noop | |
end | |
end | |
__END__ | |
Am using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0] | |
and rspec 2.11.0 | |
> rspec spec/bar_spec.rb | |
DevOps | |
#monitor called (FAILED - 1) | |
#monitor not called | |
Failures: | |
1) DevOps#monitor called | |
Failure/Error: monitor.ping('DS1') | |
NoMethodError: | |
undefined method `ping' for #<Proc:0x00000101a6d630> | |
# ./spec/bar_spec.rb:12:in `check' | |
# ./spec/bar_spec.rb:34:in `block (2 levels) in <top (required)>' | |
Finished in 0.00128 seconds | |
2 examples, 1 failure | |
Failed examples: | |
rspec ./spec/bar_spec.rb:28 # DevOps#monitor called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment