require "spec/spec_helper" module RR describe DoubleInjection do before do @space = Space.new @object = Object.new @object.methods.should_not include(method_name.to_s) @double_injection = @space.double_injection(@object, method_name) end describe "normal methods" do def method_name :foobar end describe "where the double takes a block" do it "executes the block" do method_fixture = Object.new class << method_fixture def method_with_block(a, b) yield(a, b) end end double = @space.double(@double_injection) double.with(1, 2).implemented_by(method_fixture.method(:method_with_block)) @object.foobar(1, 2) {|a, b| [b, a]}.should == [2, 1] end end describe "where there are no doubles with duplicate ArgumentExpectations" do it "dispatches to Double that have an exact match" do double1_with_exact_match = @space.double(@double_injection) double1_with_exact_match.with(:exact_match_1).returns {:return_1} double_with_no_match = @space.double(@double_injection) double_with_no_match.with("nothing that matches").returns {:no_match} double2_with_exact_match = @space.double(@double_injection) double2_with_exact_match.with(:exact_match_2).returns {:return_2} @object.foobar(:exact_match_1).should == :return_1 @object.foobar(:exact_match_2).should == :return_2 end it "dispatches to Double that have a wildcard match" do double_with_wildcard_match = @space.double(@double_injection) double_with_wildcard_match.with_any_args.returns {:wild_card_value} double_with_no_match = @space.double(@double_injection) double_with_no_match.with("nothing that matches").returns {:no_match} @object.foobar(:wildcard_match_1).should == :wild_card_value @object.foobar(:wildcard_match_2, 3).should == :wild_card_value end end describe "where there are doubles" do it "raises DoubleNotFoundError error when arguments do not match a double" do double_1 = @space.double(@double_injection) double_1.with(1, 2) double_2 = @space.double(@double_injection) double_2.with(3) error = nil begin @object.foobar(:arg1, :arg2) viotated "Error should have been raised" rescue Errors::DoubleNotFoundError => e error = e end error.message.should include("On object #