hi I''m using restful_authentication and have controller specs working using users fixtures (and login_as) however i''ve been trying for hours without success to do it without fixtures, using mocks and stubs instead. anyone here done this? got code? :) thanks
Hi Jonathan, You should be able to stub the routines a controller uses to check if a user is logged in, which will allow your controllers to function without actually requiring you to post login details. Something like ''controller.stub!(:logged_in?).and_return(true)'' should suffice. Or you could stub the before_filter method itself. Check to see what your code calls behind the scenes. You may find you need to stub routines in the authentication module depending on what you want to spec (e.g. action on failing login, action on incorrect authorization, logging in via cookie, etc). Rgds, Jerry Jonathan Linowes wrote:> hi > I''m using restful_authentication > and have controller specs working using users fixtures (and login_as) > > however i''ve been trying for hours without success to do it without > fixtures, using mocks and stubs instead. > > anyone here done this? got code? > :) > > thanks > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
My observation is that mocking or stubbing as high in the API as possible is best. So, if you have to dive into someone else''s code, your spec could break if their code breaks (e.g., restful_authentication). That''s why Jerry''s suggestion that you stub your own filter is great. Steve On May 30, 2007, at 6:22 AM, Jerry West wrote:> Hi Jonathan, > > You should be able to stub the routines a controller uses to check > if a > user is logged in, which will allow your controllers to function > without > actually requiring you to post login details. Something like > ''controller.stub!(:logged_in?).and_return(true)'' should suffice. > Or you > could stub the before_filter method itself. Check to see what your > code calls behind the scenes. You may find you need to stub > routines in > the authentication module depending on what you want to spec (e.g. > action on failing login, action on incorrect authorization, logging in > via cookie, etc). > > Rgds, > Jerry > > > > Jonathan Linowes wrote: >> hi >> I''m using restful_authentication >> and have controller specs working using users fixtures (and login_as) >> >> however i''ve been trying for hours without success to do it without >> fixtures, using mocks and stubs instead. >> >> anyone here done this? got code? >> :) >> >> thanks >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersSteve Ross sross at calicowebdev.com http://www.calicowebdev.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070530/b8f8c22b/attachment-0001.html
Jonathan Linowes
2007-May-31 03:09 UTC
[rspec-users] stubbing controller methods, I''m stumped
I must be dense because I keep having trouble stubbing anything in a controller. I can stub models but not controller methods, whether directly defined in the controller file or in a module Here''s a small example: # sandbox_controller.rb class SandboxController < ApplicationController def someaction @value = somemethod end def somemethod return 10 end end # sandbox_controller_spec.rb require File.dirname(__FILE__) + ''/../spec_helper'' describe SandboxController, " handling someaction" do before do SandboxController.stub!(:somemethod).and_return(99) end it "should get 99" do get :someaction assigns[:value].should equal(99) end end # console $ script/spec spec/controllers/sandbox_controller_spec.rb --color -- format specdoc SandboxController handling someaction - should get 99 (FAILED - 1) 1) ''SandboxController handling someaction should get 99'' FAILED expected 99, got 10 (using .equal?) ./spec/controllers/sandbox_controller_spec.rb:10: script/spec:4: Finished in 0.13381 seconds 1 example, 1 failure In real life I want to stub out the authentication methods in module / lib/authenticated_system.rb , or the before_filter method as suggested to me earlier so I can move forward with my spec''ing controllers that have authorization built in. This is getting painful. linoj
Jonathan Linowes
2007-May-31 03:12 UTC
[rspec-users] stubbing controller methods, I''m stumped
btw, if you''re so inclined, I''m on irc as "linoj" :) On May 30, 2007, at 11:09 PM, Jonathan Linowes wrote:> I must be dense because I keep having trouble stubbing anything in > a controller. I can stub models but not controller methods, whether > directly defined in the controller file or in a module > > Here''s a small example: > > # sandbox_controller.rb > > class SandboxController < ApplicationController > def someaction > @value = somemethod > end > > def somemethod > return 10 > end > end > > # sandbox_controller_spec.rb > > require File.dirname(__FILE__) + ''/../spec_helper'' > > describe SandboxController, " handling someaction" do > before do > SandboxController.stub!(:somemethod).and_return(99) > end > > it "should get 99" do > get :someaction > assigns[:value].should equal(99) > end > end > > # console > > $ script/spec spec/controllers/sandbox_controller_spec.rb --color -- > format specdoc > > SandboxController handling someaction > - should get 99 (FAILED - 1) > > 1) > ''SandboxController handling someaction should get 99'' FAILED > expected 99, got 10 (using .equal?) > ./spec/controllers/sandbox_controller_spec.rb:10: > script/spec:4: > > Finished in 0.13381 seconds > > 1 example, 1 failure > > > In real life I want to stub out the authentication methods in > module /lib/authenticated_system.rb , or the before_filter method > as suggested to me earlier so I can move forward with my spec''ing > controllers that have authorization built in. This is getting painful. > > linoj > > >
David Chelimsky
2007-May-31 03:19 UTC
[rspec-users] stubbing controller methods, I''m stumped
On 5/30/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:> I must be dense because I keep having trouble stubbing anything in a > controller. I can stub models but not controller methods, whether > directly defined in the controller file or in a module > > Here''s a small example: > > # sandbox_controller.rb > > class SandboxController < ApplicationController > def someaction > @value = somemethod > end > > def somemethod > return 10 > endThis is an instance method.> end > > # sandbox_controller_spec.rb > > require File.dirname(__FILE__) + ''/../spec_helper'' > > describe SandboxController, " handling someaction" do > before do > SandboxController.stub!(:somemethod).and_return(99)This is stubbing a class method, which doesn''t exists. Try this: controller.stub!(:somemethod).and_return(99) Cheers, David> end > > it "should get 99" do > get :someaction > assigns[:value].should equal(99) > end > end > > # console > > $ script/spec spec/controllers/sandbox_controller_spec.rb --color -- > format specdoc > > SandboxController handling someaction > - should get 99 (FAILED - 1) > > 1) > ''SandboxController handling someaction should get 99'' FAILED > expected 99, got 10 (using .equal?) > ./spec/controllers/sandbox_controller_spec.rb:10: > script/spec:4: > > Finished in 0.13381 seconds > > 1 example, 1 failure > > > In real life I want to stub out the authentication methods in module / > lib/authenticated_system.rb , or the before_filter method as > suggested to me earlier so I can move forward with my spec''ing > controllers that have authorization built in. This is getting painful. > > linoj > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Jonathan Linowes
2007-May-31 05:53 UTC
[rspec-users] stubbing controller methods (unstumped)
thanks!! My confusion was aggravated by a bug in my controller so I thought the stub wasn''t working when it was. Other problems included things like forgetting a "?" on methods like "logged_in?" ... Writing the test examples like below really helped me sort it out. I''ve blogged it at http://www.vaporbase.com/postings/ Using_Rspec_on_Controllers linoj On May 30, 2007, at 11:19 PM, David Chelimsky wrote:> On 5/30/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: >> I must be dense because I keep having trouble stubbing anything in a >> controller. I can stub models but not controller methods, whether >> directly defined in the controller file or in a module >> >> Here''s a small example: >> >> # sandbox_controller.rb >> >> class SandboxController < ApplicationController >> def someaction >> @value = somemethod >> end >> >> def somemethod >> return 10 >> end > > This is an instance method. > >> end >> >> # sandbox_controller_spec.rb >> >> require File.dirname(__FILE__) + ''/../spec_helper'' >> >> describe SandboxController, " handling someaction" do >> before do >> SandboxController.stub!(:somemethod).and_return(99) > > This is stubbing a class method, which doesn''t exists. Try this: > > controller.stub!(:somemethod).and_return(99) > > Cheers, > David > >> end >> >> it "should get 99" do >> get :someaction >> assigns[:value].should equal(99) >> end >> end >> >> # console >> >> $ script/spec spec/controllers/sandbox_controller_spec.rb --color -- >> format specdoc >> >> SandboxController handling someaction >> - should get 99 (FAILED - 1) >> >> 1) >> ''SandboxController handling someaction should get 99'' FAILED >> expected 99, got 10 (using .equal?) >> ./spec/controllers/sandbox_controller_spec.rb:10: >> script/spec:4: >> >> Finished in 0.13381 seconds >> >> 1 example, 1 failure >> >> >> In real life I want to stub out the authentication methods in >> module / >> lib/authenticated_system.rb , or the before_filter method as >> suggested to me earlier so I can move forward with my spec''ing >> controllers that have authorization built in. This is getting >> painful. >> >> linoj >> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users