[rspec-users] Some tips for testing respond_to block and making controller tests work with simply_helpful

Lourens Naude lourens at rentacarhire.com
Fri Nov 24 14:29:57 EST 2006


Yes, just hacked it up quick and posted as I needed it *yesterday*.
I'll have a look at trunk later today or tomorrow.

There's some issues with the render rspec assertions when using  
content types:

  # controller action
   def show
     respond_to do |format|
       format.html # show.rhtml
       format.xml  { render :xml => @coupon.to_xml }

# specs

   setup do
     get :show, {:locale => 'en', :id => 1 }, :html

   specify "should render show.rhtml" do
      controller.should_have_rendered :action => 'show'   
#specification fails

   specify "should render show.rhtml" do
     controller.should_have_rendered( :action =>  
"show", :content_type => Mime::HTML )  #this one passes, needs the  
explicit content type when testing actions with respond_to blocks

  #It would also be nice to have  
controller_instance.should_redirect_to 'something' pickup RJS  
redirects eg. page.redirect_to target_url()  as well

   #HTML content type
   specify "should redirect back to plans" do
     controller.should_redirect_to 'http://www.rentacars.com/en/app/ 
     get :new, { :plan_id => 20, :locale => 'en'}, :html

  #JS content type
    setup do
     @order_before = Account.find(2).coupons.collect(&:id)
     xhr :put, :sort, { :coupons => { 0 => '1',
                                      1 => '2',
                                      2 => '4' }, :locale => 'en'}

#ugly ugly hack
   specify "should redirect to coupons" do
      response.body.should_include 'http://account.rentacars.com/en/ 
      response.body.should_include 'window.location.href'

This would ideally we covered by the should_redirect_to assertion and  
not the RJS specific ones.
This redirect is client side, however, and can't be picked up  
matching response code 302 ... and thus wouldn't work in isolated  
controller tests (without view integration).
I'll look into this as well.

- Lourens

On 2006/11/24, at 06:12, David Chelimsky wrote:

> Hey Lourens,
> Thanks for these blog posts!
> Just an FYI - the rails plugin is a moving target right now
> (especially its innards) so the monkey patching that you recommended
> is all going to break in the 0.7.3 release. If you look at the trunk
> you'll see that the dependency on ZenTest is gone and we're renaming
> modules and classes to better align w/ the rest of RSpec.
> If you're finding deficiencies in the tool we'd love to hear that
> feedback in the form of feature requests and/or patches. If you're
> interested, give http://rspec.rubyforge.org/contribute.html a read and
> have at it.
> Cheers,
> David
> On 11/23/06, Lourens Naude <lourens at rentacarhire.com> wrote:
>> Hi,
>> Here's two blog posts for those that need to test respond_to blocks
>> and/or are currently using simply_helpful extensively.
>> http://blog.methodmissing.com/2006/11/22/testing-different-content-
>> types-with-rspec
>> http://blog.methodmissing.com/2006/11/23/using-simply_helpful-with-
>> rspec-controller-tests
>> - Lourens
>> _______________________________________________
>> 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

More information about the rspec-users mailing list