Skip to content

Instantly share code, notes, and snippets.

@IdahoEv
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save IdahoEv/0b628c9f2d6b2385a0fe to your computer and use it in GitHub Desktop.

Select an option

Save IdahoEv/0b628c9f2d6b2385a0fe to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe "items#index", :type => :request do
let :date do
Date.today
end
let! :item_1 do
Item.create( :description => "Give a coding demo",
:due_date => date
)
end
let! :item_2 do
Item.create( :description => "Write demo code",
:due_date => date
)
end
describe "GET /items" do
it "shows item as json" do
json_get "/items"
expect(response).to be_success
expect(response.body).to have_json_path("links")
expect(response.body).to have_json_path("links/self")
expect(response.body).to have_json_path("data")
expect(response.body).to have_json_path("data/0/data/description")
expect(response.body).to have_json_path("data/0/data/due_date")
expect(response.body).to have_json_path("data/1/data/description")
expect(response.body).to have_json_path("data/1/data/due_date")
expect(response.body).to be_json_eql("\"Give a coding demo\"").at_path("data/0/data/description")
expect(response.body).to be_json_eql("\"Write demo code\"").at_path("data/1/data/description")
end
end
end
class MenuListSerializer < BaseSerializer
def as_json_without_wrap(options={})
object.map do |menu|
ItemSerializer.new(menu).as_json
end
end
def links
{ :self => routes.items_path }
end
end
class ItemSerializer < BaseSerializer
attributes :description, :due_date
def links
{ :self => routes.item_path(object) }
end
end
require 'spec_helper'
describe "items#show", :type => :request do
let :date do
Date.today
end
let :item do
Item.create( :description => "Give a coding demo",
:due_date => date
)
end
describe "GET /item/:id" do
it "shows item as json" do
json_get "/items/#{item.id}"
expect(response).to be_success
expect(response.body).to have_json_path("links")
expect(response.body).to have_json_path("links/self")
expect(response.body).to have_json_path("data")
expect(response.body).to have_json_path("data/description")
expect(response.body).to have_json_path("data/due_date")
expect(response.body).to be_json_eql("\"Give a coding demo\"").at_path("data/description")
end
end
end
class ItemsController < ApplicationController
respond_to :json
def show
@item = Item.find(params[:id])
render :json => ItemSerializer.new(@item)
end
def index
render :json => ItemListSerializer.new(Item.all)
end
end
resources :items, :only => [ :show, :index, :create ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment