Request a non-HTML format in controller specs

Posted Over 13 years ago. Visible to the public.

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

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

Henning Koch
Last edit
About 7 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2010-10-27 10:41)