Created
September 27, 2012 06:27
-
-
Save deepak/3792506 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 | |
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 } | |
it "#monitor called", :wip => true do | |
monitor = devops.monitor | |
# TODO: does not work | |
devops.stub(:monitor) do | |
monitor.should_receive(:ping).with('DS1').and_return(stub.as_null_object) | |
end | |
devops.check | |
end | |
it "#monitor not called" do | |
# TODO: at_least(0) does not work | |
# NOTE: wrong syntax. but this api for stub feels more natural to me | |
devops.stub(:monitor) do |x| | |
x.should_receive(:ping).with('DS1').and_return(stub.as_null_object) | |
end | |
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