Experiences on the Front Lines of User Interfaces and Web Development

Rails functional tests - unexpected 406 responses

Fun discovery today of confusing behavior with Rails functional test framework (at least version 2.3.4):

### controller ###
def create  
  comment = Comment.create!(:name => params[:name])

  @result = { :id  => comment.id }

  respond_to do |format|
    format.xml  { render }
    format.json { render :json => @result }
  end
end  
### functional test ###
def test_a_method  
  post :create, :name => "foo", :format => :json
  assert_response :success

  ...
end  

The above will fail on the assert_response with a 406 instead of a 200 success.

It's due to the :json symbol!

This works great:

def test_a_method  
  post :create, :name => "foo", :format => "json"
  assert_response :success

   ...
end  

Of course, remember that your params should be strings in your functional tests too... never rely on symbols such as:

post :create, :name => :this_should_not_be_a_string  

Comments

Thanks so much, this saved me a lot of time!
Anu Mulukutla

comments powered by Disqus