Les Tests avec Ruby on Rails et RSpec (in French)
Post on 22-Nov-2014
6993 Views
Preview:
DESCRIPTION
Transcript
Les Tests avec Ruby on RailsJean-Michel Garnier <jean-michel arobase 21croissants point com>
Paris On Rails – 10 Décembre 2007
2Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Plan de la présentation
● Background– De 1975 ... à nos jours
● Théorie– RSpec par l'exemple
– Design, Spécifications, Documentation
● Pratique– Outils: Autotest, rcov, CruiseControl.rb
– RSpec on Rails, Selenium
Background
Qui est-ce?
FRED BROOKS
1975:
“The Mythical Man-Month”
● ¼ Spécificier
● 1/6 Programmer
● ½ Tests
1986:
“No Silver Bullet”
Modèle en V =
Vers le Modèle Itératifhttp://www.cs.unc.edu/~brooks/
Qui est-ce?
http://www.threeriversinstitute.org/Kent%20Beck.htm
1994:
● “Simple Smalltalk Testing”
Framework pour automatiser les tests unitaires
2002:
“Test Driven Development”
Ecrire les tests avant le code
KENT BECK
Qui est-ce?
MARTIN FOWLER
2000:● “Refactoring”● article sur l'Intégration
Continue
http://www.martinfowler.com
L'équipe de RSpec
● Début 2004 : BDD● Août 2005: RSpec 0.1● Mai 2007: RSpec 1.0● 30 250 downloads
(nº27) RubyForge● 40 contributeurs
Aslak Hellesøy (NO) David Chelimsky(US)
Dan North (UK)
+ Brian Takita Dave Astels Steve Baker Luke Redpath
Livres pour 2008
Théorie
Anatomie d'une spécificationC
ompo
rtem
ent (
cont
exte
)
Exemple
Exemple
should I stay or should I go?
RSpec ajoute 2 méthodes à la class 'Object': should et should_not
Avec Test::Unit :
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Langage naturel (en VO)
ruby spec/project_spec.rb
Résultat
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Customizer Rspec
Pas très lisible!
Solution: écrire son propre “Matcher”
http://martinfowler.com/articles/mocksArentStubs.html
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Tests = Design
● TDD = On ne code que l'indispensable!
● Le Refactoring (Améliorer le code) ne provoque pas de regressions
● Utilisation de “mocks objects” pour définir l'API
● Le code de qualité est facile à tester
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Tests = Spécifications
Tests = Documentation
● Exemples exécutable = Documentation toujours á jour
● Plus besoin de rdoc ;-)
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Pratique
Démarrage
● Peep Code: RSpec (in english)
● IDE: RSpec intégré avec:
● Installer les plugins rspec et rspec_on_rails
● rake spec:generate
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Autotest
● Problème: les specs s'executent pendant 10 min...
● Solution: Autotest n'execute que les specs nécessaires
Notifications visuelles (plugins Growl, Notify,...) et sonores
● Intégré avec
http://ph7spot.com/articles/getting_started_with_autotest
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Garantir la couverture avec rcov
● sudo gem install rcov
● rake spec:rcov
http://eigenclass.org/hiki.rb?rcov
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Couverture détaillée
Intégration Continue
http://cruisecontrolrb.thoughtworks.com
Le lapin il est ROUGE?Le Build il est cassé!
cruise add <name> -u <url>cruise start
http://nabaztag.rubyforge.org/
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
CruiseControl.rb
Exemple de spec/models
http://edgerepo.com/svn/opensource/ruby/plugins/rspec_expectation_matchers/
spec/models (méthodes Métier)
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
spec/models (Finders)
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
specs/fixtures
● Quelque problèmes: associations, lisibilité
● Mais ça s'améliore dans Rails 2.0 (un peu)
● Alternatives:– Fixture scenarios: http://errtheblog.com/post/7708
– Tests exemplars:
http://evang.eli.st/blog/2007/8/8/doin-the-fixtures-plugin-thing
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Exemple de spec/controllers
Autres “matchers“
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Spécifier les Vues?
Problème:
Les vues changent tout le temps !
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Exemple de spec/views
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Selenium
● Tests d'intégration + automatiser les tests d'acceptation des utilisateurs
● Vérifier si l'application fonctionne avec tous les butineurs du marché
● Compatible avec:
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Selenium – le film
Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Selenium specs
90% de la spec GENERE AVEC SELENIUM-IDE !!!
42Paris On Rail 2007 – Copyright (c) Garnier Jean-Michel. Licence: Creative Commons.
Merci de votre attention!
Questions - Réponses
top related