[rspec-devel] [ rspec-Feature Requests-5719 ] Improve Rails controller spec helpers

noreply at rubyforge.org noreply at rubyforge.org
Thu Nov 9 08:40:12 EST 2006

Feature Requests item #5719, was opened at 2006-09-11 06:56
You can respond by visiting: 

Category: rails plugin
Group: None
>Status: Closed
Priority: 3
Submitted By: Nobody (None)
Assigned to: Nobody (None)
Summary: Improve Rails controller spec helpers

Initial Comment:
RSpec is missing a number of features necessary for controller specs:

We badly need an rspec equivalent of assert_redirected_to.
There's response.redirect_url.should_be 'http://test.host/assets', but this is rather fragile, and liable to break depending on your routes setup.
should_redirect_to {:action => 'index'} would be nice.

Being able to use named routes would be extremely useful - eg  
"get accounts_url"
"delete account_url(@account)"
(though it's perhaps worth noting that this doesn't even work in Rails' functional tests - you have to use integration tests if you want this to work)

Some sort of routes testing is needed.  Rails's functional tests use this : 
assert_recognizes({:controller => 'assets', :action => 'new'}, 
                      {:path => "/assets/new", :method => :get})
to verify that a given path maps to the appropriate controller & action.

I'll add more as I find them.  I'm willing to help with this if I can - I tried implementing my own should_redirect_to as a wrapper around assert_redirected_to, but started to run into problems with not knowing enough about how Rails sets up the test process... 


>Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-09 08:40

Fixed and moved to other RFEs


Comment By: David Chelimsky (dchelimsky)
Date: 2006-11-05 13:51

Jonathon - 

0.7 will have controller.should_redirect_to, which you can set in advance of an action.

There is already an RFE for spec'ing routes:


And I've added one for supporting named routes:


In the future, please multiple features in separate RFEs to make it easier for us to track everything.

Thank you!



Comment By: Jonathan del Strother (catfish)
Date: 2006-09-12 12:00

ahhh, I'v just found  response.redirected_to.should_equal :action=>'index'
which works fine.     IMO should_redirect_to is much nicer-looking, but given I can't figure out how to do it without ugly hacks...


Comment By: Jonathan del Strother (catfish)
Date: 2006-09-11 07:49

FWIW, here's a horrible hacked version of should_redirect_to:

  def should_redirect_to(redirect_target)
    #need to set up a results object for the assertion.  It requires an add_assertion method...
    extras = Module.new {}
    extras.send('define_method', 'add_assertion', Proc.new{})    
    @_result = "unused"
      assert_redirected_to redirect_target
    rescue Test::Unit::AssertionFailedError => e
      spec_message = e.message.sub("response is not a redirection", "response should be a redirection")

I'm assuming there's a better way of doing this, but suspect that it involves rewriting Rails' Test Process module, which makes my head hurt.  I'll freely admit that I can't quite figure out the @_result object which seems to be required for calling assert methods, too...


You can respond by visiting: 

More information about the rspec-devel mailing list