SlideShare ist ein Scribd-Unternehmen logo
1 von 9
CURSO DE TESTING OSL
        12 – 16 DE ABRIL 2010




                           Shoulda


Alberto Perdomo

Web:       http://albertoperdomo.net
Email:     alberto.perdomo@aentos.es
Twitter:   @albertoperdomo             http://www.aentos.com
TEST::UNIT

→ los tests no tienen mucha expresividad



→ las posibilidades son limitadas



→ acabamos probando las mismas cosas una y otra vez
SHOULDA

→ librería que “amplía” Test::Unit


→ helpers para contexto y expresividad


→ macros


→ helpers
SHOULDA: CONTEXTO Y
               EXPRESIVIDAD
                     class UserTest < Test::Unit::TestCase
                       context "A User instance" do
                         setup do
                           @user = User.find(:first)
                         end

                         should "return its full name" do
                           assert_equal 'John Doe', @user.full_name
                         end

                         context "with a profile" do
                           setup do
                             @user.profile = Profile.find(:first)
                           end

                           should "return true when sent #has_profile?" do
                             assert @user.has_profile?
                           end
                         end
                       end
                     end


Produce los siguientes tests unitarios:

 "test: A User instance should return its full name."
 "test: A User instance with a profle should return true when sent #has_profle?."
SHOULDA: MACROS PARA
   ACTIVERECORD
  class PostTest < Test::Unit::TestCase
    fixtures :all

    should_belong_to :user
    should_have_many :tags, :through => :taggings

    should_validate_uniqueness_of :title
    should_validate_presence_of :body, :message => /wtf/
    should_validate_presence_of :title
    should_validate_numericality_of :user_id
  end

  class UserTest < Test::Unit::TestCase
    should_have_many :posts

    should_not_allow_values_for :email, "blah", "b lah"
    should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
    should_ensure_length_in_range :email, 1..100
    should_ensure_value_in_range :age, 1..100
    should_not_allow_mass_assignment_of :password
  end
MACROS PARA ACTIVERECORD

 test "a transfer without a from_account should not be valid" do
   transfer = Transfer.new(:from_account => nil, :to_account => @account_b, :amount => 100)
   assert !transfer.valid?
 end




                         should_validate_presence_of :from_account
SHOULDA: MACROS PARA
   CONTROLADORES

  class PostsControllerTest < ActionController::TestCase
    context "on GET to :show for first record" do
      setup do
        get :show, :id => 1
      end

      should_assign_to :user
      should_respond_with :success
      should_render_template :show
      should_not_set_the_flash

      should "do something else really cool" do
        assert_equal 1, assigns(:user).id
      end
    end
  end
SHOULDA: ASSERTIONS



   assert_same_elements([:a, :b, :c], [:c, :a, :b])
   assert_contains(['a', '1'], /d/)
   assert_contains(['a', '1'], 'a')
¿PREGUNTAS?

Weitere ähnliche Inhalte

Was ist angesagt?

Able to find out apps password in r12
Able to find out apps password in r12Able to find out apps password in r12
Able to find out apps password in r12
Vasu Nec
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
okelloerick
 
PowerShell Script to add multiple users to a group in AD
PowerShell Script to add multiple users to a group in ADPowerShell Script to add multiple users to a group in AD
PowerShell Script to add multiple users to a group in AD
Joseph Daramola
 
Book integrated assignment
Book integrated assignmentBook integrated assignment
Book integrated assignment
Akash gupta
 

Was ist angesagt? (18)

Web2py
Web2pyWeb2py
Web2py
 
Mule esb How to convert from Object to Json in 5 minutes
Mule esb How to convert from Object to Json in 5 minutesMule esb How to convert from Object to Json in 5 minutes
Mule esb How to convert from Object to Json in 5 minutes
 
Able to find out apps password in r12
Able to find out apps password in r12Able to find out apps password in r12
Able to find out apps password in r12
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
 
Mule esb - How to convert from Json to Object in 5 minutes
Mule esb - How to convert from Json to Object in 5 minutesMule esb - How to convert from Json to Object in 5 minutes
Mule esb - How to convert from Json to Object in 5 minutes
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
JavaScript patterns
JavaScript patternsJavaScript patterns
JavaScript patterns
 
Migrating legacy data
Migrating legacy dataMigrating legacy data
Migrating legacy data
 
ActiveRecord
ActiveRecordActiveRecord
ActiveRecord
 
Muleesbobjecttojson
Muleesbobjecttojson Muleesbobjecttojson
Muleesbobjecttojson
 
Mule esb object_to_json
Mule esb object_to_jsonMule esb object_to_json
Mule esb object_to_json
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
 
Web 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHPWeb 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHP
 
PowerShell Script to add multiple users to a group in AD
PowerShell Script to add multiple users to a group in ADPowerShell Script to add multiple users to a group in AD
PowerShell Script to add multiple users to a group in AD
 
Book integrated assignment
Book integrated assignmentBook integrated assignment
Book integrated assignment
 
Php mysql connectivity
Php mysql connectivityPhp mysql connectivity
Php mysql connectivity
 
Examplecode
ExamplecodeExamplecode
Examplecode
 
Mapping and listing in mule
Mapping and listing in muleMapping and listing in mule
Mapping and listing in mule
 

Andere mochten auch

Logo edarco
Logo edarcoLogo edarco
Logo edarco
elchavin
 
Boost your productivity!: Productivity tips for rails developers - Lightning ...
Boost your productivity!: Productivity tips for rails developers - Lightning ...Boost your productivity!: Productivity tips for rails developers - Lightning ...
Boost your productivity!: Productivity tips for rails developers - Lightning ...
Alberto Perdomo
 
Celine dion all by myself
Celine dion   all by myselfCeline dion   all by myself
Celine dion all by myself
elchavin
 
Tutorial goear
Tutorial goearTutorial goear
Tutorial goear
elchavin
 
Primeros pasos con la base de datos de grafos Neo4j
Primeros pasos con la base de datos de grafos Neo4jPrimeros pasos con la base de datos de grafos Neo4j
Primeros pasos con la base de datos de grafos Neo4j
Alberto Perdomo
 

Andere mochten auch (16)

Curso TDD Ruby on Rails #04: Factorías de objetos
Curso TDD Ruby on Rails #04: Factorías de objetosCurso TDD Ruby on Rails #04: Factorías de objetos
Curso TDD Ruby on Rails #04: Factorías de objetos
 
Logo edarco
Logo edarcoLogo edarco
Logo edarco
 
Boost your productivity!: Productivity tips for rails developers - Lightning ...
Boost your productivity!: Productivity tips for rails developers - Lightning ...Boost your productivity!: Productivity tips for rails developers - Lightning ...
Boost your productivity!: Productivity tips for rails developers - Lightning ...
 
Celine dion all by myself
Celine dion   all by myselfCeline dion   all by myself
Celine dion all by myself
 
Reshet Nrn Audio1
Reshet Nrn Audio1Reshet Nrn Audio1
Reshet Nrn Audio1
 
Leveraging relations at scale with Neo4j
Leveraging relations at scale with Neo4jLeveraging relations at scale with Neo4j
Leveraging relations at scale with Neo4j
 
Tutorial goear
Tutorial goearTutorial goear
Tutorial goear
 
Complaint free teachers
Complaint free teachersComplaint free teachers
Complaint free teachers
 
Svym Update Presentation
Svym Update PresentationSvym Update Presentation
Svym Update Presentation
 
Primeros pasos con la base de datos de grafos Neo4j
Primeros pasos con la base de datos de grafos Neo4jPrimeros pasos con la base de datos de grafos Neo4j
Primeros pasos con la base de datos de grafos Neo4j
 
Pie Plano
Pie PlanoPie Plano
Pie Plano
 
Rails for Mobile Devices @ Conferencia Rails 2011
Rails for Mobile Devices @ Conferencia Rails 2011Rails for Mobile Devices @ Conferencia Rails 2011
Rails for Mobile Devices @ Conferencia Rails 2011
 
Curso TDD Ruby on Rails #03: Tests unitarios
Curso TDD Ruby on Rails #03: Tests unitariosCurso TDD Ruby on Rails #03: Tests unitarios
Curso TDD Ruby on Rails #03: Tests unitarios
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
 
JOP Programs 2008-09
JOP Programs 2008-09JOP Programs 2008-09
JOP Programs 2008-09
 
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
 

Ähnlich wie Curso TDD Ruby on Rails #05: Shoulda

Coffeescript a z
Coffeescript a zCoffeescript a z
Coffeescript a z
Starbuildr
 
Антипаттерны модульного тестирования
Антипаттерны модульного тестированияАнтипаттерны модульного тестирования
Антипаттерны модульного тестирования
MitinPavel
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::Class
Curtis Poe
 

Ähnlich wie Curso TDD Ruby on Rails #05: Shoulda (20)

Spock
SpockSpock
Spock
 
Rails workshop for Java people (September 2015)
Rails workshop for Java people (September 2015)Rails workshop for Java people (September 2015)
Rails workshop for Java people (September 2015)
 
php part 2
php part 2php part 2
php part 2
 
Why Our Code Smells
Why Our Code SmellsWhy Our Code Smells
Why Our Code Smells
 
Double Trouble
Double TroubleDouble Trouble
Double Trouble
 
Ruby basic3
Ruby basic3Ruby basic3
Ruby basic3
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Python advance
Python advancePython advance
Python advance
 
Coffeescript a z
Coffeescript a zCoffeescript a z
Coffeescript a z
 
Антипаттерны модульного тестирования
Антипаттерны модульного тестированияАнтипаттерны модульного тестирования
Антипаттерны модульного тестирования
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Why ruby
Why rubyWhy ruby
Why ruby
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::Class
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012
 

Mehr von Alberto Perdomo (6)

Curso TDD Ruby on Rails #08: Buenas prácticas
Curso TDD Ruby on Rails #08: Buenas prácticasCurso TDD Ruby on Rails #08: Buenas prácticas
Curso TDD Ruby on Rails #08: Buenas prácticas
 
Curso TDD Ruby on Rails #02: Test Driven Development
Curso TDD  Ruby on Rails #02: Test Driven DevelopmentCurso TDD  Ruby on Rails #02: Test Driven Development
Curso TDD Ruby on Rails #02: Test Driven Development
 
Curso TDD Ruby on Rails #06: Mocks y stubs
Curso TDD Ruby on Rails #06: Mocks y stubsCurso TDD Ruby on Rails #06: Mocks y stubs
Curso TDD Ruby on Rails #06: Mocks y stubs
 
Curso TDD Ruby on Rails #02: Test Driven Development
Curso TDD Ruby on Rails #02: Test Driven DevelopmentCurso TDD Ruby on Rails #02: Test Driven Development
Curso TDD Ruby on Rails #02: Test Driven Development
 
Curso TDD Ruby on Rails #01: Introducción al testing
Curso TDD Ruby on Rails #01: Introducción al testingCurso TDD Ruby on Rails #01: Introducción al testing
Curso TDD Ruby on Rails #01: Introducción al testing
 
Plugins de autenticación en Rails - Lightning talk Las Palmas On Rails 09/02/...
Plugins de autenticación en Rails - Lightning talk Las Palmas On Rails 09/02/...Plugins de autenticación en Rails - Lightning talk Las Palmas On Rails 09/02/...
Plugins de autenticación en Rails - Lightning talk Las Palmas On Rails 09/02/...
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Curso TDD Ruby on Rails #05: Shoulda

  • 1. CURSO DE TESTING OSL 12 – 16 DE ABRIL 2010 Shoulda Alberto Perdomo Web: http://albertoperdomo.net Email: alberto.perdomo@aentos.es Twitter: @albertoperdomo http://www.aentos.com
  • 2. TEST::UNIT → los tests no tienen mucha expresividad → las posibilidades son limitadas → acabamos probando las mismas cosas una y otra vez
  • 3. SHOULDA → librería que “amplía” Test::Unit → helpers para contexto y expresividad → macros → helpers
  • 4. SHOULDA: CONTEXTO Y EXPRESIVIDAD class UserTest < Test::Unit::TestCase context "A User instance" do setup do @user = User.find(:first) end should "return its full name" do assert_equal 'John Doe', @user.full_name end context "with a profile" do setup do @user.profile = Profile.find(:first) end should "return true when sent #has_profile?" do assert @user.has_profile? end end end end Produce los siguientes tests unitarios: "test: A User instance should return its full name." "test: A User instance with a profle should return true when sent #has_profle?."
  • 5. SHOULDA: MACROS PARA ACTIVERECORD class PostTest < Test::Unit::TestCase fixtures :all should_belong_to :user should_have_many :tags, :through => :taggings should_validate_uniqueness_of :title should_validate_presence_of :body, :message => /wtf/ should_validate_presence_of :title should_validate_numericality_of :user_id end class UserTest < Test::Unit::TestCase should_have_many :posts should_not_allow_values_for :email, "blah", "b lah" should_allow_values_for :email, "a@b.com", "asdf@asdf.com" should_ensure_length_in_range :email, 1..100 should_ensure_value_in_range :age, 1..100 should_not_allow_mass_assignment_of :password end
  • 6. MACROS PARA ACTIVERECORD test "a transfer without a from_account should not be valid" do transfer = Transfer.new(:from_account => nil, :to_account => @account_b, :amount => 100) assert !transfer.valid? end should_validate_presence_of :from_account
  • 7. SHOULDA: MACROS PARA CONTROLADORES class PostsControllerTest < ActionController::TestCase context "on GET to :show for first record" do setup do get :show, :id => 1 end should_assign_to :user should_respond_with :success should_render_template :show should_not_set_the_flash should "do something else really cool" do assert_equal 1, assigns(:user).id end end end
  • 8. SHOULDA: ASSERTIONS assert_same_elements([:a, :b, :c], [:c, :a, :b]) assert_contains(['a', '1'], /d/) assert_contains(['a', '1'], 'a')