Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed broken feature related to multiple method stubs and added anoth…

…er for any_instance.stub.with - this should close Issue #58
  • Loading branch information...
commit e542b1845dd88c742c9d6050027327a69f077d37 1 parent 3c129ae
@kaiwren kaiwren authored
View
19 features/method_stubs/any_instance.feature
@@ -37,4 +37,23 @@ Feature: stub on any instance of a class
end
"""
When I run `rspec example_spec.rb`
+ Then the examples should all pass
+
+ Scenario: any_instance stub with specific arguments matchers
+ Given a file named "example_spec.rb" with:
+ """
+ describe "any_instance.stub" do
+ context "with arguments" do
+ it "returns the stubbed value when arguments match" do
+ Object.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
+ Object.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)
+
+ o = Object.new
+ o.foo(:param_one, :param_two).should eq(:result_one)
+ o.foo(:param_three, :param_four).should eq(:result_two)
+ end
+ end
+ end
+ """
+ When I run `rspec example_spec.rb`
Then the examples should all pass
View
4 lib/rspec/mocks/any_instance.rb
@@ -11,10 +11,6 @@ def any_instance
RSpec::Mocks::space.add(self)
__recorder
end
-
- def exactly_one_instance
- any_instance
- end
def rspec_verify
__recorder.verify
View
2  lib/rspec/mocks/any_instance/message_chains.rb
@@ -5,7 +5,7 @@ class MessageChains < Hash
def add(method_name, chain)
(self[method_name] ||= []) << chain
end
-
+
def has_expectation?(method_name)
!!self[method_name].find{|chain| chain.is_a?(ExpectationChain)}
end
View
24 lib/rspec/mocks/any_instance/recorder.rb
@@ -2,6 +2,7 @@ module RSpec
module Mocks
module AnyInstance
class Recorder
+ attr_reader :message_chains
def initialize(klass)
@message_chains = MessageChains.new
@observed_methods = []
@@ -9,15 +10,22 @@ def initialize(klass)
@klass = klass
@expectation_set = false
end
-
- def message_chains
- @message_chains
- end
- def stub(method_name, *args, &block)
- observe!(method_name)
- message_chains.add(method_name, chain = StubChain.new(method_name, *args, &block))
- chain
+ def stub(method_name_or_method_map, *args, &block)
+ if method_name_or_method_map.is_a?(Hash)
+ method_map = method_name_or_method_map
+ method_map.each do |method_name, return_value|
+ observe!(method_name)
+ message_chains.add(method_name, chain = StubChain.new(method_name))
+ chain.and_return(return_value)
+ end
+ method_map
+ else
+ method_name = method_name_or_method_map
+ observe!(method_name)
+ message_chains.add(method_name, chain = StubChain.new(method_name, *args, &block))
+ chain
+ end
end
def should_receive(method_name, *args, &block)
View
17 spec/rspec/mocks/any_instance_spec.rb
@@ -1,3 +1,4 @@
+
require 'spec_helper'
module RSpec
@@ -55,7 +56,21 @@ def another_existing_method; end
klass.any_instance.stub(:foo)
lambda{ klass.new.bar }.should raise_error(NoMethodError)
end
-
+
+ context 'multiple methods' do
+ it "allows multiple methods to be stubbed in a single invocation" do
+ klass.any_instance.stub(:foo => 'foo', :bar => 'bar')
+ instance = klass.new
+ instance.foo.should eq('foo')
+ instance.bar.should eq('bar')
+ end
+
+ it "adheres to the contract of multiple method stubbing withou any instance" do
+ Object.new.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
+ klass.any_instance.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
+ end
+ end
+
context "behaves as 'every instance'" do
it "stubs every instance in the spec" do
klass.any_instance.stub(:foo).and_return(result = Object.new)
Please sign in to comment.
Something went wrong with that request. Please try again.