Forum: RSpec Problems stubbing @controller.stub!(:send)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b7459aca37bf49900de66dc2113e163?d=identicon&s=25 David Schmidt (Guest)
on 2008-11-04 22:27
(Received via mailing list)
In one of my controller tests I'm testing a method which uses
self.send(<private method name>) to do some pre-processing if that
private method name is defined:

             # If this task requires some additional preperation then
create a
             # private method below with the same name as the
task_type and
             # it will be executed here.
             if private_methods.include?(task_type)
               self.send(task_type)
             end

I will be testing these private_methods separately so I want to just
stub out the "self.send" line so that I can verify that it's called if
there's a match.

If I just stub out :send in my before() method:

       @controller.stub!(:send)

then it appears that a *previous* send is stubbed, and my method never
gets tested (shown by my first should not getting hit).

If I specify a "with" for that stub:

       @controller.stub!(:send).with('private method')

I then get a "stack level too deep" error.

I have similar problems when attempt to use the following instead of
the before/stub:

     it "should run any private methods if task_type matches" do
       @mock_task.should_receive(:run).and_return(@mock_task)
       @controller.should_receive(:send).with('private method')
       do_request
     end

Here I get the error message "Mock 'TasksController' expected :send
with ("private method" but received it with (:perform_action)"

Is there any way that I can stub out *just* the :send with "private
method" and leave the other one alone to execute?

Thank you,

David Schmidt
davids@tower-mt.com
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-11-04 22:56
(Received via mailing list)
David Schmidt <davids@tower-mt.com> writes:

>               self.send(task_type)
> then it appears that a *previous* send is stubbed, and my method never
>
> method" and leave the other one alone to execute?
I'm not sure, but you should be able to stub out :private_method and it
ought to work fine, even when you send it.

Pat
6b7459aca37bf49900de66dc2113e163?d=identicon&s=25 David Schmidt (Guest)
on 2008-11-05 01:34
(Received via mailing list)
On Nov 4, 2008, at 1:53 PM, Pat Maddox wrote:

>>            # it will be executed here.
>>            if private_methods.include?(task_type)
>>              self.send(task_type)
>>            end
>>
>> I will be testing these private_methods separately so I want to just
>> stub out the "self.send" line so that I can verify that it's called
>> if
>> there's a match.
>>
>> [...]

>> Is there any way that I can stub out *just* the :send with "private
>> method" and leave the other one alone to execute?
>
> I'm not sure, but you should be able to stub out :private_method and
> it
> ought to work fine, even when you send it.
>
> Pat

That did the trick and worked great.  Thanks Pat!

David Schmidt
davids@tower-mt.com
This topic is locked and can not be replied to.