Posted over 9 years ago. Visible to the public.

Request a non-HTML format in controller specs

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:

Copy
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

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

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 3 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more