[rspec-devel] [ rspec-Bugs-8165 ] Partial Mock Errors when respond_to? is true but the method is not in the object

noreply at rubyforge.org noreply at rubyforge.org
Tue Jan 30 18:56:05 EST 2007


Bugs item #8165, was opened at 2007-01-26 02:59
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=8165&group_id=797

>Category: mock module
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: Brian Takita (btakita)
>Assigned to: David Chelimsky (dchelimsky)
Summary: Partial Mock Errors when respond_to? is true but the method is not in the object

Initial Comment:
Unfortunately, this happens with ActiveRecord until #define_read_methods is called.

dir = File.dirname(__FILE__)
require "#{dir}/spec_helper"

context "An object where respond_to? is true and does not have method" do
  specify "should not raise an exception" do
    obj = Object.new
    obj.should_receive(:respond_to?).with(:foobar).and_return(true)
    obj.should_receive(:foobar).and_return(:baz)
    obj.foobar.should == :baz
  end
end


Here is an ActiveRecord case:

context "An object where respond_to? is true and does not have method" do
  specify "should not raise an exception" do
    obj = Person.new
    obj.should_receive(:respond_to?).with(:foobar).and_return(true)
    obj.should_receive(:foobar).and_return(:baz)
    obj.foobar.should == :baz
  end
end

----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-01-30 23:56

Message:
The problem was that MockHandler uses :respond_to? to decide whether or not to proxy a method. Since the spec says "respond true to respond_to?(:foobar), the object tells MockHandler "yes, I do respond to :foobar" so MockHandler tries to proxy it. Since the object LIED (because the spec told it to), it threw an error when MockHandler tried to alias the non-existent :foobar method.

Fixed in rev 1449.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=8165&group_id=797


More information about the rspec-devel mailing list