Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Le refactoring en confiance

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
crohr
January 09, 2013

Le refactoring en confiance

Écriture de tests boîte noire pour du code “legacy” avec RSpec, Capybara & Selenium

crohr

January 09, 2013

Transcript

20 slides
  1. Text Le  refactoring   en  confiance Écriture  de  tests  boîte

     noire  pour  du  code  “legacy” avec  RSpec,  Capybara  &  Selenium Cyril  Rohr  -­‐  @crohr  -­‐  h@p://crohr.me DevCamp#4,  Rennes,  Jan.2013
  2. • Rails  2.0.2  (20/12/2007) • Ruby  1.8.6 • 60kloc •

    De@e  technique  accumulée   depuis  6  ans “Legacy”
  3. • Tests • Gros  clients,  mises  à  jour   quoddiennes

    • Stack  technique  ancienne • Code  complexe
  4. Blackbox  tes2ng RSpec Capybara Selenium Rails2.0.2 app http://localhost:3001 Test DB

    SUT FactoryGirl DatabaseCleaner Runner But  :  assurer  un  minimum   de  confiance  lors  du   refactoring  
  5. Demo cd path/to/app ./script/server -p 3001 -e test cd path/to/blackbox

    bundle exec rspec spec/ Lancer le SUT Lancer le Runner
  6. • Ac3veRecord Accès  à  la  base • FactoryGirl Gesdon  des

     fixtures • DatabaseCleaner RAZ  de  la  base  entre   chaque  test • RSpec Test  framework  for   code • Capybara Test  framework  for  web   apps • Selenium Web  browser   automadon Ou2ls
  7. require 'spec_helper' describe "Mobile Applications", :driver => :mobile do let!(:user)

    { create(:admin_user, roles: ['message_manager']) } let!(:site) { create(:site, account: user.account) } context "with at least one site with a live version" do before do login_as(user.login) visit "/applications" end it "should view a list of applications" do page.should have_content("Your applications") end it "should see the application name" do page.should have_content(site.name) end end end Tests  -­‐  RSpec  &  FactoryGirl FactoryGirl.define do factory :site do sequence(:name) {|i| "SiteName#{i}"} account end end
  8. Configura2on  -­‐  Capybara require 'capybara/rspec' Capybara.register_driver :mobile do |app| args

    = [] args << "--window-size=320,480" args << "--user-agent='Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'" Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => args) end PORT = ENV.fetch('PORT') { 3001 } Capybara.configure do |config| config.default_driver = :chrome config.javascript_driver = :chrome config.run_server = false config.app_host = ENV.fetch('HOST') { "http://localhost:#{PORT}" } config.save_and_open_page_path = "tmp" end https://github.com/jnicklas/capybara
  9. • Assure  un  minimum  de  confiance  lors   du  refactoring

    • Structure  indépendante  du  projet • Non  limité  par  la  stack  existante • Indépendant  du  langage • Enfin  des  tests  pour  du  “legacy  code” Avantages

More Decks by crohr

Other Decks in Programming