Minitest: Testing JSON APIs
Need to set a format for a controller test? Simple the format
def test_index_returns_all_companies
get :index, :format => :json
end
Then in your controller you can do this ...
class CompaniesController < ApplicationController
respond_to :json
def index
@companies = Company.all
end
end
Then in your view...
json.array!(@companies) do |company|
json.extract! company, :id, :name, :description, :image_url
json.branches company.branches, :id, :name
json.products company.products, :id, :name, :description, :image_url
end
Written by Trevor Kimenye
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#