[rspec-users] Problems stubbing @controller.stub!(:send)

David Schmidt davids at tower-mt.com
Tue Nov 4 15:51:02 EST 2008


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 at tower-mt.com





More information about the rspec-users mailing list