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 book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
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)