Eric Harris-Braun
2008-Sep-09 00:33 UTC
[rspec-users] help on skipping a before_filter for a story
Hi folks, I''m hoping for a bit of help on best-practices for skipping a before_filter when running a particular step. Specifically the authentication filter. What happens is that the post (see code below) returns a redirect response to the login page triggered by the of my authentication filter, rather than the contents of what I''d like to be testing. How do people handle temporarily turning of this kind of thing that''s not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV == ''test'' after it, but obviouly that won''t work for the specs that actually test that before filter! Thanks for any help! -Eric Given "$field in new entry is $field_value" do |field,field_value| @params ||= {} @params[field.intern] = field_value end When "submitting the new entry" do post "/entry", :record => @params end Then "should include confirmation: $message" do |message| response.should have_text(/#{message}/) end -- He who is content with his lot probably has a lot.
Eric Harris-Braun
2008-Sep-09 00:49 UTC
[rspec-users] help on skipping a before_filter for a story
Hi folks, I''m hoping for a bit of help on best-practices for skipping a before_filter when running a particular step. Specifically the authentication filter. What happens is that the post (see code below) returns a redirect response to the login page triggered by the of my authentication filter, rather than the contents of what I''d like to be testing. How do people handle temporarily turning of this kind of thing that''s not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV == ''test'' after it, but obviouly that won''t work for the specs that actually test that before filter! Thanks for any help! -Eric Given "$field in new entry is $field_value" do |field,field_value| @params ||= {} @params[field.intern] = field_value end When "submitting the new entry" do post "/entry", :record => @params end Then "should include confirmation: $message" do |message| response.should have_text(/#{message}/) end -- He who is content with his lot probably has a lot.
Ben Mabey
2008-Sep-09 01:23 UTC
[rspec-users] help on skipping a before_filter for a story
Eric Harris-Braun wrote:> Hi folks, > > I''m hoping for a bit of help on best-practices for skipping a > before_filter when running a particular step. Specifically the > authentication filter. What happens is that the post (see code below) > returns a redirect response to the login page triggered by the of my > authentication filter, rather than the contents of what I''d like to be > testing. > > How do people handle temporarily turning of this kind of thing that''s > not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV > == ''test'' after it, but obviouly that won''t work for the specs that > actually test that before filter! > > Thanks for any help! > > -Eric > > Given "$field in new entry is $field_value" do |field,field_value| > @params ||= {} > @params[field.intern] = field_value > end > > When "submitting the new entry" do > post "/entry", :record => @params > end > > Then "should include confirmation: $message" do |message| > response.should have_text(/#{message}/) > end > >Hi Eric, The point of stories is to have them act as integration tests going through the entire stack just like a typical user would. So, you should really be logging in to your app before you start submitting forms. So doing something like: When "submitting the new entry" do User.create!(:login => ''james'', :password => ''password'') post "/sessions", :login => ''james'', :passsword => ''password'' post "/entry", :record => @params end I typically extract this out into a helper called ''login'' where I can simply pass a user object. BTW, have you checked out webrat yet? It makes submitting forms very easy. HTH, Ben
David Chelimsky
2008-Sep-09 01:45 UTC
[rspec-users] help on skipping a before_filter for a story
On Mon, Sep 8, 2008 at 7:49 PM, Eric Harris-Braun <gbs at panix.com> wrote:> Hi folks, > > I''m hoping for a bit of help on best-practices for skipping a > before_filter when running a particular step. Specifically the > authentication filter. What happens is that the post (see code below) > returns a redirect response to the login page triggered by the of my > authentication filter, rather than the contents of what I''d like to be > testing. > > How do people handle temporarily turning of this kind of thing that''s > not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV > == ''test'' after it, but obviouly that won''t work for the specs that > actually test that before filter!Hi Eric, Story Runner and Cucumber both hook into rails through ActionController::Integration::Session which, as far as I know, offers no hooks the likes of which you are looking for. The get, post, put and delete methods are not the same as those in rails functional tests or rspec controller specs because they do not target a specific controller - they actually go through routing - so there is no way to get a handle on the controller on which you want to bypass the filter. For me, this is as it should be. The idea behind automating scenarios is to run through the stack, including routing. Even when we bypass routing and controllers and go directly to models, we''re still going from the outside of the model layer - not to its internals. That said, if you still want to do this and control when it happens, I *think* you could do something like this: class ApplicationController def self.skipping_authentication alias_method :orig_authenticate, :authenticate def authenticate; true; end yield alias_method :authenticate, :orig_authenticate end end ApplicationController.skipping_authentication do post "/entry", :record => @params end I have not tested it in a story. It might not work. But it might :) I did try the concept out outside of rails/stories/etc: http://gist.github.com/9597 That''ll output this: BAR bar HTH, David> Thanks for any help! > > -Eric > > Given "$field in new entry is $field_value" do |field,field_value| > @params ||= {} > @params[field.intern] = field_value > end > > When "submitting the new entry" do > post "/entry", :record => @params > end > > Then "should include confirmation: $message" do |message| > response.should have_text(/#{message}/) > end > > -- > He who is content with his lot probably has a lot. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Jonathan Linowes
2008-Sep-09 02:47 UTC
[rspec-users] help on skipping a before_filter for a story
On Sep 8, 2008, at 8:49 PM, Eric Harris-Braun wrote:> Hi folks, > > I''m hoping for a bit of help on best-practices for skipping a > before_filter when running a particular step. Specifically the > authentication filter. What happens is that the post (see code below) > returns a redirect response to the login page triggered by the of my > authentication filter, rather than the contents of what I''d like to be > testing. > > How do people handle temporarily turning of this kind of thing that''s > not relevant to the test? Temporarily I''ve just put an unless > RAILS_ENV > == ''test'' after it, but obviouly that won''t work for the specs that > actually test that before filter! > > Thanks for any help! > > -Eric > > Given "$field in new entry is $field_value" do |field,field_value| > @params ||= {} > @params[field.intern] = field_value > end > > When "submitting the new entry" do > post "/entry", :record => @params > end > > Then "should include confirmation: $message" do |message| > response.should have_text(/#{message}/) > endIf your controller has this :before_filter :login_required and say, login_required is defined in application.rb, then your controller spec can stub it out controller.stub!(:login_required).and_return(true)
David Chelimsky
2008-Sep-09 02:51 UTC
[rspec-users] help on skipping a before_filter for a story
On Mon, Sep 8, 2008 at 9:47 PM, Jonathan Linowes <jonathan at parkerhill.com> wrote:> > On Sep 8, 2008, at 8:49 PM, Eric Harris-Braun wrote: > >> Hi folks, >> >> I''m hoping for a bit of help on best-practices for skipping a >> before_filter when running a particular step. Specifically the >> authentication filter. What happens is that the post (see code below) >> returns a redirect response to the login page triggered by the of my >> authentication filter, rather than the contents of what I''d like to be >> testing. >> >> How do people handle temporarily turning of this kind of thing that''s >> not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV >> == ''test'' after it, but obviouly that won''t work for the specs that >> actually test that before filter! >> >> Thanks for any help! >> >> -Eric >> >> Given "$field in new entry is $field_value" do |field,field_value| >> @params ||= {} >> @params[field.intern] = field_value >> end >> >> When "submitting the new entry" do >> post "/entry", :record => @params >> end >> >> Then "should include confirmation: $message" do |message| >> response.should have_text(/#{message}/) >> end > > If your controller has this > > :before_filter :login_required > > and say, login_required is defined in application.rb, then your controller > spec can stub it out > > controller.stub!(:login_required).and_return(true)This is true in a controller spec, but Eric was asking about story steps where this won''t work. Cheers, David
Jonathan Linowes
2008-Sep-09 02:56 UTC
[rspec-users] help on skipping a before_filter for a story
never mind, i misread your question, thought you were talking about a controller spec rather than a story stories are intended to exercise the full stack, so I actually log in, with a step like this: Given "I am logged in " do user = create_registered_user( :login => ''user'', :password => ''secret'') post_via_redirect "/sessions", :login => ''user'', :password => "secret", response.should be_success session[:user].should == user.id end On Sep 8, 2008, at 10:47 PM, Jonathan Linowes wrote:> > On Sep 8, 2008, at 8:49 PM, Eric Harris-Braun wrote: > >> Hi folks, >> >> I''m hoping for a bit of help on best-practices for skipping a >> before_filter when running a particular step. Specifically the >> authentication filter. What happens is that the post (see code >> below) >> returns a redirect response to the login page triggered by the of my >> authentication filter, rather than the contents of what I''d like >> to be >> testing. >> >> How do people handle temporarily turning of this kind of thing that''s >> not relevant to the test? Temporarily I''ve just put an unless >> RAILS_ENV >> == ''test'' after it, but obviouly that won''t work for the specs that >> actually test that before filter! >> >> Thanks for any help! >> >> -Eric >> >> Given "$field in new entry is $field_value" do |field,field_value| >> @params ||= {} >> @params[field.intern] = field_value >> end >> >> When "submitting the new entry" do >> post "/entry", :record => @params >> end >> >> Then "should include confirmation: $message" do |message| >> response.should have_text(/#{message}/) >> end > > If your controller has this > > :before_filter :login_required > > and say, login_required is defined in application.rb, then your > controller spec can stub it out > > controller.stub!(:login_required).and_return(true) > >
Eric Harris-Braun
2008-Sep-09 03:49 UTC
[rspec-users] help on skipping a before_filter for a story
Thanks David for the very detailed response. I see what you mean about why this should be hard to do. In fact perhaps what I should do instead is simply specify the user-login part in my story instead of skipping it. I didn''t at first because its a pain, i.e. sending the sessions/create post and then saving the sessions cookie and then sending it along with the post I really want to test. Are there any best-practices or examples of how folks accomplish this task? Thanks, -Eric -- Earth is a beta site.
David Chelimsky
2008-Sep-09 03:51 UTC
[rspec-users] help on skipping a before_filter for a story
On Mon, Sep 8, 2008 at 10:49 PM, Eric Harris-Braun <gbs at panix.com> wrote:> Thanks David for the very detailed response. > > I see what you mean about why this should be hard to do. In fact > perhaps what I should do instead is simply specify the user-login part > in my story instead of skipping it. I didn''t at first because its a > pain, i.e. sending the sessions/create post and then saving the sessions > cookie and then sending it along with the post I really want to test. > > Are there any best-practices or examples of how folks accomplish this > task?You should not need to save any cookies. You should be able to simply log in and then go to the secure page. Have you tried just doing that?> > Thanks, > > -Eric > -- > Earth is a beta site. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Matt Wynne
2008-Sep-17 19:32 UTC
[rspec-users] help on skipping a before_filter for a story
We just write a step that says ''Given I am logged in'' which actually walks through the login process on the screens (fills_in :username etc). However you could have another step like ''Given authenication is disabled on the site'' which does something dirty to your ApplicationController using mocks to replace the function you''re calling from the before filter with a mocked response. Something like ApplicationController.should_receive(:authenticate).and_return(true) I don''t think is is recommended though as these are meant to be full- stack integration tests - using mocking is a bad habit to get into. On 9 Sep 2008, at 01:33, Eric Harris-Braun wrote:> Hi folks, > > I''m hoping for a bit of help on best-practices for skipping a > before_filter when running a particular step. Specifically the > authentication filter. What happens is that the post (see code below) > returns a redirect response to the login page triggered by the of my > authentication filter, rather than the contents of what I''d like to be > testing. > > How do people handle temporarily turning of this kind of thing that''s > not relevant to the test? Temporarily I''ve just put an unless > RAILS_ENV > == ''test'' after it, but obviouly that won''t work for the specs that > actually test that before filter! > > Thanks for any help! > > -Eric > > Given "$field in new entry is $field_value" do |field,field_value| > @params ||= {} > @params[field.intern] = field_value > end > > When "submitting the new entry" do > post "/entry", :record => @params > end > > Then "should include confirmation: $message" do |message| > response.should have_text(/#{message}/) > end > > -- > He who is content with his lot probably has a lot. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, Matt ---- http://blog.mattwynne.net http://songkick.com In case you wondered: The opinions expressed in this email are my own and do not necessarily reflect the views of any former, current or future employers of mine.
aslak hellesoy
2008-Sep-17 19:58 UTC
[rspec-users] help on skipping a before_filter for a story
On Tue, Sep 9, 2008 at 2:33 AM, Eric Harris-Braun <eric at harris-braun.com> wrote:> Hi folks, > > I''m hoping for a bit of help on best-practices for skipping aThere are no practices for this (or anything else) that are better than any other practice.> before_filter when running a particular step. Specifically the > authentication filter. What happens is that the post (see code below) > returns a redirect response to the login page triggered by the of my > authentication filter, rather than the contents of what I''d like to be > testing. >My general advice is to not futz around with your application''s behaviour when you''re running stories (or cucumber features). Why? Because stories/features are intended to be end-to-end tests of the unaltered app. This was recently discussed in a thread about mocking/stubbing from steps. If the application wants you to log in, then log in from one of your steps. If for whatever reason this is impractical I would recommend doing something like: unless ENV[''SKIP_LOGIN_IN_STORIES''] -and set that env var from one of the ruby files that get loaded before your story/feature runs. (but I would consider this a test smell) Cheers, Aslak> How do people handle temporarily turning of this kind of thing that''s > not relevant to the test? Temporarily I''ve just put an unless RAILS_ENV > == ''test'' after it, but obviouly that won''t work for the specs that > actually test that before filter! > > Thanks for any help! > > -Eric > > Given "$field in new entry is $field_value" do |field,field_value| > @params ||= {} > @params[field.intern] = field_value > end > > When "submitting the new entry" do > post "/entry", :record => @params > end > > Then "should include confirmation: $message" do |message| > response.should have_text(/#{message}/) > end > > -- > He who is content with his lot probably has a lot. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >