1. API-based client/serverAPI-based client/server
applications with TDD andapplications with TDD and
ActiveResourceActiveResource
Wolfram ArnoldWolfram Arnold
www.rubyfocus.bizwww.rubyfocus.biz
In collaboration with:In collaboration with:
Kurt SnyderKurt Snyder
papercheck.compapercheck.com
marakana.commarakana.com
5. Client
Application
(Flash or JS)
Frontend ScalabilityFrontend Scalability
Client
Application
ActiveResource
Server
Application
API
JSON
DB
SQL
Client
Application
(Mobile)
6. Rails ActiveResourceRails ActiveResource
Model objects that behaves ActiveRecord objects
Maps CRUD operations to RESTful API
No Associations
No Validations (added in Rails 3)
Rougher around the edges than ActiveRecord
7. From CRUD to RESTFrom CRUD to REST
class Person < ActiveResource::Base
site “http://server.example.com/people”
format :json
end
Person.create(:name => “Joe”)
→ POST /people, {'name':'Joe'}
Person.find(4)
→ GET /people/4
8. Resourceful RoutesResourceful Routes
map.resources :people (in config/routes.rb)
people_path, people_url “named route methods”
GET /people → “index” action
POST /people → “create” action
new_person_path, new_person_url
GET /people/new → “new” action
edit_person_path, edit_person_url
GET /people/:id/edit → “edit” action with ID
person_path, person_url
GET /people/:id → “show” action with ID
PUT /people/:id → “update” action with ID
DELETE /people/:id → “destroy” action with ID
12. TDD ActiveRecord StyleTDD ActiveRecord Style
it “should create the object” do
lambda {
Person.create(:first_name => “Joe”)
}.should change(Person, :count).by(1)
end
it “should destroy the object” do
p = Factory.create(:person)
lambda {
p.destroy
}.should change(Person, :count).by(-1)
end
14. Mocks anyone?Mocks anyone?
FakeWeb gem: http://github.com/chrisk/fakeweb
FakeWeb.register_uri(:get,
"http://server.example.com/people",
:body => "Hello World!")
Body is the mock object.
It should simulate the server response.
Great solution if it's a 3rd
party server.
15. Problems with Mocks?Problems with Mocks?
If the server code is under our control...
What happens if the server API changes but the
mock doesn't?
What process keeps the simulated server
responses and the actual API in sync?
Better ideas?
16. Beyond MocksBeyond Mocks
Remote Specs
Rely on remote server running (in test environment)
Mechanism to set up data
Mechanism to inspect data
Mechanism to clean up data
17. Distributed Ruby “DRb”Distributed Ruby “DRb”
Client
Application
DRb Client
Server
Application
DRb Server
DRb
Remote
Method
Invocation
foo(args)
foo(args)
20. SummarySummary
Server application with REST controller
Client application with ActiveResource
Use DRb to:
test objects
inquire “actual” data
TDD with ActiveResource
21. BizConfBizConf
Aug 4-6, 2010
Amelia Island, FL
Discount code: WOLF
for 43% off
http://bizconf.org?
coupon=WOLF
Rails & Web App
Professionals, Entrepreneurs,
Consultants
Small group
Network with Who's Who
Organized by Obie
Fernandez of Hashrocket
Highlight: David Allen