Skip to content

Commit

Permalink
Walkthrough 6-1: Rendering XML and JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Jen-Mei Wu committed Jan 20, 2011
1 parent 668caa0 commit 8acdd41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/controllers/hotels_controller.rb
@@ -1,9 +1,10 @@
class HotelsController < ApplicationController
respond_to :html, :xml, :json

before_filter :load_hotel, :except => [:new, :index, :create]

def index
@hotels = Hotel.order('name')
respond_with(@hotels = Hotel.order('name'))
end

def show
Expand Down
91 changes: 60 additions & 31 deletions spec/integration/hotels_spec.rb
@@ -1,50 +1,79 @@
require 'spec_helper'

describe "hotels" do
before do
@hotel1 = Factory.create(
:hotel,
:name => "The Lakes"
)
@hotel2 = Factory.create(
:hotel,
:name => "St. Clair Hotel"
)
end

describe "listing hotels" do
before do
@hotel1 = Factory.create(
:hotel,
:name => "The Lakes"
)
@hotel2 = Factory.create(
:hotel,
:name => "St. Clair Hotel"
)
visit "/hotels"
end
describe "when format is html" do
before do
visit "/hotels"
end

it "should list as many hotels as we have" do
page.all('ul li').should have(2).hotels
end
it "should list as many hotels as we have" do
page.all('ul li').should have(2).hotels
end

it "should list all hotel names (xpath)" do
page.should have_xpath(
"//li[.='#{@hotel1.name}']"
)
page.should have_xpath(
"//li[.='#{@hotel2.name}']"
)
end

it "should list all hotel names (css)" do
hotel_names = page.all('ul li').map(&:text)
hotel_names.should include(@hotel1.name)
hotel_names.should include(@hotel2.name)
end

describe "when clicking on a link" do
before do
click_link(@hotel1.name)
end

it "should show detailed information for our hotel" do
page.should have_xpath(
"//h1[.='#{@hotel1.name}']"
)
end
end

it "should list all hotel names (xpath)" do
page.should have_xpath(
"//li[.='#{@hotel1.name}']"
)
page.should have_xpath(
"//li[.='#{@hotel2.name}']"
)
end

it "should list all hotel names (css)" do
hotel_names = page.all('ul li').map(&:text)
hotel_names.should include(@hotel1.name)
hotel_names.should include(@hotel2.name)
describe "when the requested format is xml" do
before do
visit "/hotels.xml"
end

it "should render the found hotels as xml" do
page.should have_xpath('//hotels[@type="array"]')
end
end

describe "when clicking on a link" do
describe "when the requested format is json" do
before do
click_link(@hotel1.name)
visit '/hotels.json'
end

it "should show detailed information for our hotel" do
page.should have_xpath(
"//h1[.='#{@hotel1.name}']"
)
it "should render the found hotels as json" do
hotels = JSON.parse(page.body)
hotels.should be_an(Array)
hotels.first["hotel"]["name"].should == "St. Clair Hotel"
end
end

end

end

0 comments on commit 8acdd41

Please sign in to comment.