[rspec-users] Fwd: Issue with AR::Base descendants with certain argument

dchelimsky at gmail.com dchelimsky at gmail.com
Sun Oct 31 08:14:06 EDT 2010



On Oct 28, 7:40 am, Alexey Ilyichev <bluesman.a... at gmail.com> wrote:
> I have made 2 examples for this and I can't figure out how to make both
> pass. Take a look please:
>
> require 'spec_helper'
>
> describe "Mock" do
>   class A
>     def self.method_missing(name, *args, &block)
>       '*a_method_missing*'
>     end
>
>     def self.present_method(arg)
>       '*present_method*'
>     end
>   end
>
>   class B < A
>     def self.method_missing(name, *args, &block)
>       '*b_method_missing*'
>     end
>   end
>
>   it 'should call derived class method_missing when args differ from what is
> stubbed' do
>     B.stub!(:missing_method).with(1).and_return '*stub*'
>     B.missing_method(2).should == '*b_method_missing*'
>   end
>
>   it 'should call present_method when it is defined in parent class and args
> differ from what is stubbed' do
>     B.stub!(:present_method).with(1).and_return '*stub*'
>     B.present_method(2).should == '*present_method*'
>   end
> end
>
> ---------- Forwarded message ----------
> From: Alexey Ilyichev <bluesman.a... at gmail.com>
> Date: Thu, Oct 28, 2010 at 12:44 PM
> Subject: Issue with AR::Base descendants with certain argument
> To: rspec-us... at rubyforge.org
>
> Hi!
>
> I am trying to upgrade rspec-rails to 1.3.3, and one of my specs fails.
>
> In before :each I stub finder method like this:
> Payment.stub!(:find_by_id).with(@payment.id.to_s).and_return @payment
> Payment class is derived from AR::Base
>
> And then in one of the examples I call Payment.find_by_id((@payment.id +
> 1).to_s), and I am expecting it to return null, as it used to with 1.2.9
> version, however it leads to an exception in active_record/base.rb.
> Analyzing the stack trace I figured that in line 115 of mock/proxy.rb method
> find_by_id is called for ActiveRecord::Base, which is not correct.
>
> I'm not familiar with rspec internals, so I don't have an idea on where in
> code the actual problem is. So any help would be greatly appreciated.

The expected behaviour is that when you stub or mock a method on a
real object (we call this partial stubbing/mocking), the framework
takes over that method for the duration of the example. So the
expectation that when the wrong args are received the call is
delegated to the original object is incorrect, and making this change
would introduce potential problems for other users who count on this
behaviour.

There is a request to add an explicit way to call to the original in
rspec-mocks-2's issue tracker: http://github.com/rspec/rspec-mocks/issues#issue/23.

HTH,
David


More information about the rspec-users mailing list