Monday, September 13, 2010

Functional test for respond_to :json in Rails


# Notes controller

def index
...
  respond_to do |format|
    format.json { render :json => @notes }
  end
end

# Notes functional test

params = {:format => 'json', :title => ...}

get :index, params

notes = JSON.parse(@response.body)
assert_equal note.title, notes[0]['note']['title']

No comments: