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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)