David Schmidt
2008-Nov-04 20:51 UTC
[rspec-users] Problems stubbing @controller.stub!(:send)
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
David Schmidt <davids at tower-mt.com> writes:> 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?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
David Schmidt
2008-Nov-05 00:27 UTC
[rspec-users] Problems stubbing @controller.stub!(:send)
On Nov 4, 2008, at 1:53 PM, Pat Maddox wrote:> David Schmidt <davids at tower-mt.com> writes: > >> 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. >> >> [...]>> 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. > > PatThat did the trick and worked great. Thanks Pat! David Schmidt davids at tower-mt.com