2010 07-20 tdd with activeresource
DESCRIPTION
API-based client/server applications with TDD and ActiveResourcegithub: http://github.com/wolframarnold/tdd_with_activeresourceTRANSCRIPT
![Page 1: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/1.jpg)
API-based client/server API-based client/server applications with TDD and applications with TDD and
ActiveResource ActiveResource
Wolfram ArnoldWolfram Arnoldwww.rubyfocus.bizwww.rubyfocus.biz
In collaboration with:In collaboration with:Kurt SnyderKurt Snyder
papercheck.compapercheck.commarakana.commarakana.com
![Page 2: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/2.jpg)
Database-backedDatabase-backed
DB
Application
ActiveRecord
SQL
![Page 3: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/3.jpg)
API-backedAPI-backed
ClientApplication
ActiveResource
ServerApplication
API
JSON
DB
SQL
![Page 4: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/4.jpg)
Tradeoffs?Tradeoffs?
DB-backed
Fast
ActiveRecord well developed
Complex relationships possible
Limited scalability—tight coupling
API-backed
Extra network & server latency
Complex relationships more difficult
Scales well to different front-ends—loose coupling
![Page 5: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/5.jpg)
ClientApplication
(Flash or JS)
Frontend ScalabilityFrontend Scalability
ClientApplication
ActiveResource
ServerApplication
API
JSON
DB
SQL
ClientApplication
(Mobile)
![Page 6: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/6.jpg)
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
![Page 7: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/7.jpg)
From CRUD to RESTFrom CRUD to REST
class Person < ActiveResource::Basesite “http://server.example.com/people”format :json
end
Person.create(:name => “Joe”)
→ POST /people, {'name':'Joe'}
Person.find(4)
→ GET /people/4
![Page 8: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/8.jpg)
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
![Page 9: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/9.jpg)
Hyperactive ResourceHyperactive Resource
http://github.com/taryneast/hyperactiveresource
Adds Associations
Supports nested routes
/people/5/addresses/3
But: Cannot mix nested and non-nested routes for the same resource
Adds Validations
![Page 10: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/10.jpg)
Let's do some codingLet's do some coding
Demo
RESTful Person Server API with JSON
HyperactiveResource-based client
![Page 11: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/11.jpg)
TDD anyone?TDD anyone?
DB
Application
ActiveRecord
SQL
RSpec model testswith fixtures, factories
Test Databasewith transaction support
![Page 12: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/12.jpg)
TDD ActiveRecord StyleTDD ActiveRecord Style
it “should create the object” dolambda { Person.create(:first_name => “Joe”)}.should change(Person, :count).by(1)
end
it “should destroy the object” dop = Factory.create(:person)lambda { p.destroy}.should change(Person, :count).by(-1)
end
![Page 13: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/13.jpg)
API-backedAPI-backed
ClientApplication
ActiveResource
ServerApplication
API
JSON
DB
SQL
RSpec model test
Test DatabaseonTest Serverinstance!
![Page 14: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/14.jpg)
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.
![Page 15: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/15.jpg)
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?
![Page 16: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/16.jpg)
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
![Page 17: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/17.jpg)
Distributed Ruby “DRb”Distributed Ruby “DRb”
ClientApplication
DRb Client
ServerApplication
DRb Server
DRb
RemoteMethodInvocation
foo(args)
foo(args)
![Page 18: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/18.jpg)
Demo DRb
![Page 19: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/19.jpg)
Gotcha'sGotcha's
ActiveResource::Base.include_root_in_json
after_initialize hook for starting DRb server
:id problem for Objects, via DRb
Need to run server in test mode
Ideas/Questions:
Front Object in DRb?
rake task to start/stop server—reliably find process?
DRb port inflation—better way?
![Page 20: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/20.jpg)
SummarySummary
Server application with REST controller
Client application with ActiveResource
Use DRb to:
test objects
inquire “actual” data
TDD with ActiveResource
![Page 21: 2010 07-20 TDD with ActiveResource](https://reader031.vdocument.in/reader031/viewer/2022020207/55959e9c1a28ab622d8b460d/html5/thumbnails/21.jpg)
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