Your SlideShare is downloading. ×
Curso TDD Ruby on Rails #04: Factorías de objetos
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Curso TDD Ruby on Rails #04: Factorías de objetos

1,033

Published on

Lección 04 del curso de TDD en Ruby on Rails: …

Lección 04 del curso de TDD en Ruby on Rails:
Factorías de objetos

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,033
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. CURSO DE TESTING OSL 12 – 16 DE ABRIL 2010 Factorías de objetos Alberto Perdomo Web: http://albertoperdomo.net Email: alberto.perdomo@aentos.es Twitter: @albertoperdomo http://www.aentos.com
  • 2. PROBLEMAS DE LOS FIXTURES: DATOS ESPECÍFICOS → A veces hacen falta datos específcos para ciertas pruebas pero están disponibles para todos los tests. → Ejemplo: Hemos implementado la gestión de cobros y para probar los casos extremos necesitamos una instancia de usuario sin saldo sufciente.
  • 3. PROBLEMAS DE LOS FIXTURES: MANTENIBILIDAD → incómodos → difíciles de mantener → a menudo las relaciones acaban “rotas”.
  • 4. PROBLEMA RESUELTO: FACTORÍAS DE OBJETOS → compatibles con los fxtures → usa lo que más te guste o lo que mejor se adapte a la situación → ¡extremadamente fexibles!
  • 5. VARIAS ALTERNATIVAS Factory Girl → http://github.com/thoughtbot/factory_girl Machinist → http://github.com/notahat/machinist Object daddy → http://github.com/fogic/object_daddy Fixture Replacement → http://github.com/smtlaissezfaire/fxturereplacement y otras...
  • 6. FACTORY GIRL: DEFINICIÓN Se suelen defnir → todas dentro del mismo fchero → test/factories.rb → o en fcheros separados test/factories/user.rb etc. # This will guess the User class Factory.define :user do |u| test/factories.rb u.first_name 'John' spec/factories.rb u.last_name 'Doe' test/factories/*.rb u.admin false end spec/factories/*.rb
  • 7. FACTORY GIRL: AVANZADO Lazy attributes Factory.define :user do |u| # ... u.activation_code { User.generate_activation_code } end Dependent Attributes Factory.define :user do |u| u.first_name 'Joe' u.last_name 'Blow' u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase } end Factory(:user, :last_name => 'Doe').email # => "joe.doe@example.com"
  • 8. FACTORY GIRL: AVANZADO Asociaciones Factory.define :post do |p| # ... p.author {|author| author.association(:user, :last_name => 'Writely') } end Secuencias # Defines a new sequence Ejemplo: uso de sequence para el Factory.sequence :email do |n| email dentro de la factoría de usuarios "person#{n}@example.com" end Factory.define :user do |f| Factory.next :email f.email { Factory.next(:email) } # => "person1@example.com" end Factory.next :email # => "person2@example.com"
  • 9. FACTORY: USO BÁSICO user = Factory(:user)
  • 10. FACTORY GIRL: USO AVANZADO # Returns a User instance that's not saved user = Factory.build(:user) # Returns a saved User instance user = Factory.create(:user) # Returns a hash of attributes that can be used to build a User instance: attrs = Factory.attributes_for(:user) # Returns an object with all defined attributes stubbed out: stub = Factory.stub(:user) # Same as Factory.create :user: user = Factory(:user) # Build a User instance and override the first_name property user = Factory.build(:user, :first_name => 'Joe') user.first_name # => "Joe"
  • 11. ¿PREGUNTAS?

×