require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper") module RR describe DoubleInjection do attr_reader :space, :subject, :double_injection it_should_behave_like "Swapped Space" before do @subject = Object.new subject.methods.should_not include(method_name.to_s) @double_injection = space.double_injection(subject, method_name) end def new_double Double.new( double_injection, DoubleDefinitions::DoubleDefinition.new( DoubleDefinitions::DoubleDefinitionCreator.new, subject ).any_number_of_times ) end describe "methods whose name do not contain ! or ?" do def method_name :foobar end context "when the original method uses the passed-in block" do it "executes the passed-in block" do method_fixture = Object.new class << method_fixture def method_with_block(a, b) yield(a, b) end end double = new_double double.definition.with(1, 2).implemented_by(method_fixture.method(:method_with_block)) subject.foobar(1, 2) {|a, b| [b, a]}.should == [2, 1] end end context "when no other Double with duplicate ArgumentExpectations exists" do it "dispatches to Double that have an exact match" do double1_with_exact_match = new_double double1_with_exact_match.definition.with(:exact_match_1).returns {:return_1} double_with_no_match = new_double double_with_no_match.definition.with("nothing that matches").returns {:no_match} double2_with_exact_match = new_double double2_with_exact_match.definition.with(:exact_match_2).returns {:return_2} subject.foobar(:exact_match_1).should == :return_1 subject.foobar(:exact_match_2).should == :return_2 end it "dispatches to Double that have a wildcard match" do double_with_wildcard_match = new_double double_with_wildcard_match.definition.with_any_args.returns {:wild_card_value} double_with_no_match = new_double double_with_no_match.definition.with("nothing that matches").returns {:no_match} subject.foobar(:wildcard_match_1).should == :wild_card_value subject.foobar(:wildcard_match_2, 3).should == :wild_card_value end end context "when other Doubles exists but none of them match the passed-in arguments" do it "raises DoubleNotFoundError error when arguments do not match a double" do double_1 = new_double double_1.definition.with(1, 2) double_2 = new_double double_2.definition.with(3) error = nil begin subject.foobar(:arg1, :arg2) viotated "Error should have been raised" rescue Errors::DoubleNotFoundError => e error = e end error.message.should include("On subject #