Last Updated: February 25, 2016
·
2.632K
· kimenye

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