require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper") module RR describe DoubleInjection do attr_reader :subject, :method_name, :double_injection macro("sets up object and method_name") do it "sets up object and method_name" do double_injection.subject.should === subject double_injection.method_name.should == method_name.to_sym end end describe "#initialize" do context "when method_name is a symbol" do send("sets up object and method_name") before do @subject = Object.new @method_name = :foobar subject.methods.should_not include(method_name.to_s) @double_injection = DoubleInjection.new(subject, method_name) end end context "when method_name is a string" do send("sets up object and method_name") before do @subject = Object.new @method_name = 'foobar' subject.methods.should_not include(method_name) @double_injection = DoubleInjection.new(subject, method_name) end end context "when method does not exist on object" do before do @subject = Object.new @method_name = :foobar subject.methods.should_not include(method_name.to_s) end context "and invoking the method for the first time does not add the method" do before do @double_injection = DoubleInjection.new(subject, method_name) end send("sets up object and method_name") example "object does not have original method" do double_injection.object_has_original_method?.should be_false end end context "and invoking the method for the first time adds the method" do before do class << subject def respond_to?(method_name) true end def method_missing(method_name, *args, &block) (class << self; self; end).class_eval do define_method(method_name) {} end end end @double_injection = DoubleInjection.new(subject, method_name) end send("sets up object and method_name") example "object has original method" do double_injection.object_has_original_method?.should be_true end end end context "when method exists on object" do send("sets up object and method_name") before do @subject = Object.new @method_name = :to_s subject.methods.should include(method_name.to_s) @double_injection = DoubleInjection.new(subject, method_name) end it "has a original_method" do double_injection.object_has_original_method?.should be_true end end context "when method_name is ==" do send("sets up object and method_name") before do @subject = Object.new @method_name = '==' @double_injection = DoubleInjection.new(subject, method_name) end end end end end