One of my controller actions sends a redirect if the request URI begins
with /foods/search
34 def search
35 return redirect_to "/#{params[:name]}" if
request.request_uri.match /^\/foods\/search/
Unfortunately, I can''t figure out how to spec this.
>From everything that I''ve read while researching this problem, it
seems
that #get and #post only accept the action to call, and a hash of
parameters. IE:
get :action, :some => ''param''
As a result, I can''t do this:
121 it ''redirects /foods/search/almonds to /almonds'' do
122 get ''/foods/search/almonds''
123 response.should redirect_to "/almonds"
124 end
How can I spec this?
Thanks for your help,
Nick
--
Posted via http://www.ruby-forum.com/.
On Sun, Jan 31, 2010 at 17:02, Nick Hoffman <lists at ruby-forum.com> wrote:> One of my controller actions sends a redirect if the request URI begins > with /foods/search > > ?34 ?def search > ?35 ? ?return redirect_to "/#{params[:name]}" if > request.request_uri.match /^\/foods\/search/ > > Unfortunately, I can''t figure out how to spec this.Do you want to check that search redirects, or the algorithm for computing the redirect path? I would probably check the algorithm separately, then stub the algorithm to check the redirect. -- J. B. (Joe) Rainsberger :: http://www.jbrains.ca :: http://blog.thecodewhisperer.com Diaspar Software Services :: http://www.diasparsoftware.com Author, JUnit Recipes 2005 Gordon Pask Award for contribution to Agile practice :: Agile 2010: Learn. Practice. Explore.
On 31 January 2010 22:02, Nick Hoffman <lists at ruby-forum.com> wrote:> One of my controller actions sends a redirect if the request URI begins > with /foods/search > > 34 def search > 35 return redirect_to "/#{params[:name]}" if > request.request_uri.match /^\/foods\/search/ > > Unfortunately, I can''t figure out how to spec this. > > >From everything that I''ve read while researching this problem, it seems > that #get and #post only accept the action to call, and a hash of > parameters. IE: > get :action, :some => ''param'' > As a result, I can''t do this: > 121 it ''redirects /foods/search/almonds to /almonds'' do > 122 get ''/foods/search/almonds'' > 123 response.should redirect_to "/almonds" > 124 end > > How can I spec this? > > Thanks for your help, > Nick > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >You might find this easier to do as an integration test. I''d use cucumber for this. Personally I think speccing controllers is pretty hard for the benefits it gives, and if you keep your controllers real simple and thin and use cucumber you can discard most controller specs. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100221/3f9a8d7f/attachment.html>
Extract redirect logic to an object or helper and test that.
UrlRewriter.new.rewrite(''/foods/search/almonds'').should ==
''/almonds''
helper.rewrite_url(''/foods/search/almonds'').should ==
''/almonds''
and then you will see that this should probably be called higher up in the call
stack, in a before_filter. Or even higher up, at your web server, nullifying the
need for any ruby code in the first place.
Cucumber works, too. "There''s more than one way to kill a cat
than by stuffing it with butter."
Pat
On Feb 21, 2010, at 2:52 AM, Andrew Premdas wrote:
>
>
> On 31 January 2010 22:02, Nick Hoffman <lists at ruby-forum.com>
wrote:
> One of my controller actions sends a redirect if the request URI begins
> with /foods/search
>
> 34 def search
> 35 return redirect_to "/#{params[:name]}" if
> request.request_uri.match /^\/foods\/search/
>
> Unfortunately, I can''t figure out how to spec this.
>
> >From everything that I''ve read while researching this problem,
it seems
> that #get and #post only accept the action to call, and a hash of
> parameters. IE:
> get :action, :some => ''param''
> As a result, I can''t do this:
> 121 it ''redirects /foods/search/almonds to /almonds'' do
> 122 get ''/foods/search/almonds''
> 123 response.should redirect_to "/almonds"
> 124 end
>
> How can I spec this?
>
> Thanks for your help,
> Nick
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
> You might find this easier to do as an integration test. I''d use
cucumber for this. Personally I think speccing controllers is pretty hard for
the benefits it gives, and if you keep your controllers real simple and thin and
use cucumber you can discard most controller specs.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20100221/78db96d4/attachment.html>