SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
Testing
Test Driven Development


• Implementation
• Red-Green-Refactor
• Regression Tests
Implementation


• Test what should happen
• Test what should not happen
Red-Green-Refactor
      Add a test

     Run all tests
   Write some code

  Run the tests again

    Refactor code
Regression Tests

Change environment

Change code

Is everything still working
Testing in php

$this->get(quot;http://myserver/login.phpquot;);
$this->assertWantedPattern(quot;Please login to continuequot;);

$this->setField(quot;usernamequot;, quot;MyTestUserquot;);
$this->setField(quot;passwordquot;, quot;t0ps3cr3tquot;);
$this->clickSubmit(quot;Loginquot;);

$this->assertWantedPattern(quot;You are logged inquot;);
Testing in Java

beginAt(quot;login.jspquot;);
assertTextInElement(quot;h1quot;, quot;Please login to continuequot;);

setFormElement(quot;usernamequot;, quot;MyTestUserquot;);
setFormElement(quot;passwordquot;, quot;t0ps3cr3tquot;);
submit();

assertTextInElement(quot;h1quot;, quot;You are logged inquot;);
Testing in Rails

visit login_path
assert_contain quot;Please login to continuequot;

fill_in quot;usernamequot;, :with => quot;MyTestUserquot;
fill_in quot;passwordquot;, :with => quot;t0ps3cr3tquot;
click_button quot;Loginquot;

assert_contain quot;You are logged inquot;
Test Types

• Unit
• Functional
• Integration
Unit


• Most basic level of testing
• Model tests in Rails
Unit Testing
                          class ShipTest
class Ship
                           def test_crew
 attr_accessor :captain
                             ship = Ship.new
 attr_accessor :maties
                             ship.captain = 1
                             ship.mateys = 20
 def crew
  captain + maties
                            assert_equal 21, ship.crew
 end
                           end
end
                          end
Functional


• Test lifecycle of objects
• Controller tests in Rails
Functional Testing
class ShipsController
 def enter_other_ship
  @ship = Ship.find_captain(params[:captain])
  @ship.gold += 1000
  @ship.save
  redirect_to :action => 'show_loot'
 end
end                          class ShipsControllerTest
                              def test_entering_other_ship
                               post 'enter_other_ship', :captain => quot;Sparrowquot;

                              assert_response :redirect
                              assert_equal quot;Black Pearlquot;, assign(:ship).name
                              assert_equal 1000, assigns(:ship).gold
                             end
                            end
Integration

• Overall application functionalities
• Walk through a series of events
• View Tests / Stories
Integration Testing
                                    def test_attacking_other_ships
                                     Given quot;another shipquot; do |ship|
                                      @ship_to_enter = Ship.find(ship)
                                     end

                                     Given quot;my shipquot; do
Story: Attacking other ships          @my_ship = Ship.find_by_captain(quot;Sparrowquot;)
 As Captain Sparrow                  end
 I attack another ship
 So I can buy more rum               Given /my $number_of_mateys maties/ do |number_of_mateys|
                                      @ship_to_enter.attackers = number_of_mateys
                                     end
 Scenario: Attack first ship
 Given another ship                  When quot;we attach the other shipquot; do
 And my ship                          @my_ship.attacks(@ship_to_enter)
 And my 20 mateys                    end
 When we attack the other ship
 Then 1 pirate dies                  Then /$pirates_lost pirate dies/ do |pirates_lost|
                                      assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
 And we steel 1000 pieces of gold
                                     end

                                     Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
                                      assert_equal pieces_of_gold, @my_ship.gold
                                     end
                                    end
Integration Testing
  Story: Attacking other ships
   As Captain Sparrow
   I attack another ship
   So I can buy more rum

   Scenario: Attack first ship
   Given another ship
   And my ship
   And my 20 mateys
   When we attack the other ship
   Then 1 pirate dies
   And we steel 1000 pieces of gold
def test_attacking_other_ships
 Given quot;another shipquot; do |ship|
  @ship_to_enter = Ship.find(ship)
 end

 Given quot;my shipquot; do
  @my_ship = Ship.find_by_captain(quot;Sparrowquot;)
 end

 Given /my $number_of_mateys maties/ do |number_of_mateys|
  @ship_to_enter.attackers = number_of_mateys
 end

 When quot;we attach the other shipquot; do
  @my_ship.attacks(@ship_to_enter)
 end

 Then /$pirates_lost pirate dies/ do |pirates_lost|
  assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
 end

 Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
  assert_equal pieces_of_gold, @my_ship.gold
 end
end
Test Data


• Mock/stub
• Fixtures
• Factories
Mocking & Stubbing
   def test_getting_tweet_on_homepage
    response = mock
    response.stubs(:authorization).returns(true)
    response.stubs(:last_tweet).resturns(quot;#arrrrcamp rocksquot;)
    TwitterAPI.expects(:get_last_tweet).returns(response)

    get 'index'
    assert_equal quot;#arrrrcamp rocksquot;, assigns(:tweet).body
   end



- Mocha
- Rspec
- Flex Mock
Fixtures
pirates.yml              ships.yml
 captain_jack_sparrow:   black_pearl:
  name: Jack Sparrow      name: The Black Pearl
  enemy: Royal Navy       max_crew: 85
                          captain: captain_jack_sparrow
                         interceptor:
                           name: The Interceptor
                           max_crew: 150
                           captain: captain_jack_sparrow
Factories
Factory.sequence :pirate do |n|
 quot;matey#{n}quot;
end

Factory.define :ship do |f|             should quot;only find big shipsquot; do
 f.name     'Pirateship'                Factory(:ship, :max_crew => 500)
 f.max_crew 100                         Factory(:ship, :max_crew => 200)
 f.captain { Factory.next(:pirate) }
end                                     ships = Ship.big_ones
                                        assert_equal 1, ships.size
                                       end

- Factory Girl
- Machinist
- Object Daddy
- Foundry
- Fixjour
Questions

Contenu connexe

Plus de joren de groof (10)

Git techtalk
Git techtalkGit techtalk
Git techtalk
 
Tatft
TatftTatft
Tatft
 
Something something rack
Something something rackSomething something rack
Something something rack
 
Rubyandrails
RubyandrailsRubyandrails
Rubyandrails
 
Rails Servers
Rails ServersRails Servers
Rails Servers
 
Radiant
RadiantRadiant
Radiant
 
Prawn
PrawnPrawn
Prawn
 
Mistakes
MistakesMistakes
Mistakes
 
Git
GitGit
Git
 
Cucumber
CucumberCucumber
Cucumber
 

Dernier

( Sports training) All topic (MCQs).pptx
( Sports training) All topic (MCQs).pptx( Sports training) All topic (MCQs).pptx
( Sports training) All topic (MCQs).pptxParshotamGupta1
 
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxAlbania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxWorld Wide Tickets And Hospitality
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺anilsa9823
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改atducpo
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking MenDelhi Call girls
 
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...World Wide Tickets And Hospitality
 
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...gurkirankumar98700
 
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docxSlovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docxWorld Wide Tickets And Hospitality
 
9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In GhazipurGenuineGirls
 
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Paymentanilsa9823
 
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Neil Horowitz
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking MenDelhi Call girls
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Eticketing.co
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking MenDelhi Call girls
 
🔝|97111༒99012🔝 Call Girls In {Delhi} Cr Park ₹5.5k Cash Payment With Room De...
🔝|97111༒99012🔝 Call Girls In  {Delhi} Cr Park ₹5.5k Cash Payment With Room De...🔝|97111༒99012🔝 Call Girls In  {Delhi} Cr Park ₹5.5k Cash Payment With Room De...
🔝|97111༒99012🔝 Call Girls In {Delhi} Cr Park ₹5.5k Cash Payment With Room De...Diya Sharma
 
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Eticketing.co
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024HechemLaameri
 

Dernier (20)

( Sports training) All topic (MCQs).pptx
( Sports training) All topic (MCQs).pptx( Sports training) All topic (MCQs).pptx
( Sports training) All topic (MCQs).pptx
 
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxAlbania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
 
Call Girls Service Noida Extension @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Noida Extension @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Noida Extension @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Noida Extension @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
 
Call Girls In RK Puram 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In RK Puram 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In RK Puram 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In RK Puram 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men
 
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
 
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
 
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docxSlovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
 
9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur
 
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
 
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men
 
🔝|97111༒99012🔝 Call Girls In {Delhi} Cr Park ₹5.5k Cash Payment With Room De...
🔝|97111༒99012🔝 Call Girls In  {Delhi} Cr Park ₹5.5k Cash Payment With Room De...🔝|97111༒99012🔝 Call Girls In  {Delhi} Cr Park ₹5.5k Cash Payment With Room De...
🔝|97111༒99012🔝 Call Girls In {Delhi} Cr Park ₹5.5k Cash Payment With Room De...
 
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024
 

Testing

  • 2. Test Driven Development • Implementation • Red-Green-Refactor • Regression Tests
  • 3. Implementation • Test what should happen • Test what should not happen
  • 4. Red-Green-Refactor Add a test Run all tests Write some code Run the tests again Refactor code
  • 5. Regression Tests Change environment Change code Is everything still working
  • 6. Testing in php $this->get(quot;http://myserver/login.phpquot;); $this->assertWantedPattern(quot;Please login to continuequot;); $this->setField(quot;usernamequot;, quot;MyTestUserquot;); $this->setField(quot;passwordquot;, quot;t0ps3cr3tquot;); $this->clickSubmit(quot;Loginquot;); $this->assertWantedPattern(quot;You are logged inquot;);
  • 7. Testing in Java beginAt(quot;login.jspquot;); assertTextInElement(quot;h1quot;, quot;Please login to continuequot;); setFormElement(quot;usernamequot;, quot;MyTestUserquot;); setFormElement(quot;passwordquot;, quot;t0ps3cr3tquot;); submit(); assertTextInElement(quot;h1quot;, quot;You are logged inquot;);
  • 8. Testing in Rails visit login_path assert_contain quot;Please login to continuequot; fill_in quot;usernamequot;, :with => quot;MyTestUserquot; fill_in quot;passwordquot;, :with => quot;t0ps3cr3tquot; click_button quot;Loginquot; assert_contain quot;You are logged inquot;
  • 9. Test Types • Unit • Functional • Integration
  • 10. Unit • Most basic level of testing • Model tests in Rails
  • 11. Unit Testing class ShipTest class Ship def test_crew attr_accessor :captain ship = Ship.new attr_accessor :maties ship.captain = 1 ship.mateys = 20 def crew captain + maties assert_equal 21, ship.crew end end end end
  • 12. Functional • Test lifecycle of objects • Controller tests in Rails
  • 13. Functional Testing class ShipsController def enter_other_ship @ship = Ship.find_captain(params[:captain]) @ship.gold += 1000 @ship.save redirect_to :action => 'show_loot' end end class ShipsControllerTest def test_entering_other_ship post 'enter_other_ship', :captain => quot;Sparrowquot; assert_response :redirect assert_equal quot;Black Pearlquot;, assign(:ship).name assert_equal 1000, assigns(:ship).gold end end
  • 14. Integration • Overall application functionalities • Walk through a series of events • View Tests / Stories
  • 15. Integration Testing def test_attacking_other_ships Given quot;another shipquot; do |ship| @ship_to_enter = Ship.find(ship) end Given quot;my shipquot; do Story: Attacking other ships @my_ship = Ship.find_by_captain(quot;Sparrowquot;) As Captain Sparrow end I attack another ship So I can buy more rum Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys end Scenario: Attack first ship Given another ship When quot;we attach the other shipquot; do And my ship @my_ship.attacks(@ship_to_enter) And my 20 mateys end When we attack the other ship Then 1 pirate dies Then /$pirates_lost pirate dies/ do |pirates_lost| assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers And we steel 1000 pieces of gold end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold end end
  • 16. Integration Testing Story: Attacking other ships As Captain Sparrow I attack another ship So I can buy more rum Scenario: Attack first ship Given another ship And my ship And my 20 mateys When we attack the other ship Then 1 pirate dies And we steel 1000 pieces of gold
  • 17. def test_attacking_other_ships Given quot;another shipquot; do |ship| @ship_to_enter = Ship.find(ship) end Given quot;my shipquot; do @my_ship = Ship.find_by_captain(quot;Sparrowquot;) end Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys end When quot;we attach the other shipquot; do @my_ship.attacks(@ship_to_enter) end Then /$pirates_lost pirate dies/ do |pirates_lost| assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold end end
  • 18. Test Data • Mock/stub • Fixtures • Factories
  • 19. Mocking & Stubbing def test_getting_tweet_on_homepage response = mock response.stubs(:authorization).returns(true) response.stubs(:last_tweet).resturns(quot;#arrrrcamp rocksquot;) TwitterAPI.expects(:get_last_tweet).returns(response) get 'index' assert_equal quot;#arrrrcamp rocksquot;, assigns(:tweet).body end - Mocha - Rspec - Flex Mock
  • 20. Fixtures pirates.yml ships.yml captain_jack_sparrow: black_pearl: name: Jack Sparrow name: The Black Pearl enemy: Royal Navy max_crew: 85 captain: captain_jack_sparrow interceptor: name: The Interceptor max_crew: 150 captain: captain_jack_sparrow
  • 21. Factories Factory.sequence :pirate do |n| quot;matey#{n}quot; end Factory.define :ship do |f| should quot;only find big shipsquot; do f.name 'Pirateship' Factory(:ship, :max_crew => 500) f.max_crew 100 Factory(:ship, :max_crew => 200) f.captain { Factory.next(:pirate) } end ships = Ship.big_ones assert_equal 1, ships.size end - Factory Girl - Machinist - Object Daddy - Foundry - Fixjour