Read more

Request a non-HTML format in controller specs

Henning Koch
October 27, 2010Software engineer at makandra GmbH

If a controller action responds to other formats than HTML (XML, PDF, Excel, JSON, ...), you can reach that code in a controller spec like this:

describe UsersController do
  describe '#index' do
    it 'should be able to send an excel file' do
       # stubs and expectations go here
       get :index, :format => 'xls'
    end
  end
end
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Remember that both the :format parameter and the HTTP_ACCEPT header can make a controller respond with another format.

Also, make sure to pass the desired format as a String, not a Symbol:
get :show, :id => 1, :format => 'pdf' # works
get :show, :id => 1, :format => :pdf # does not work

Posted by Henning Koch to makandra dev (2010-10-27 12:41)