views:

368

answers:

4

If your controller action looks like this:

respond_to do |format|
  format.html { raise 'Unsupported' }
  format.js # index.js.erb
end

and your functional test looks like this:

test "javascript response..." do
  get :index
end

it will execute the HTML branch of the respond_to block.

If you try this:

test "javascript response..." do
  get 'index.js'
end

it executes the view (index.js.erb) withOUT running the controller action!

+2  A: 

Use this before request:

@request.env['HTTP_ACCEPT'] = 'text/javascript'
Edgard Arakaki
+1  A: 

Set the accepted content type to the type you want:

@request.accept = "text/javascript"

Combine this with your get :index test and it will make the appropriate call to the controller.

Craig Walker
+3  A: 

Pass in a :format with your normal params to trigger a response in that format.

get :index, :format => 'js'

No need to mess with you request headers.

Squeegy
+1  A: 

with rspec:

it "should render js" do
  xhr :get, 'index'
  response.content_type.should == Mime::JS
end

and in your controller action:

respond_to do |format|
  format.js
end
Steven Soroka