SlideShare a Scribd company logo
1 of 46
Download to read offline
Testing Merb
The Right Way
Why Test?
TDD
TDD and Regressions
TDD and Regressions

             I am not saying
             TDD is bad, but
             this talk doesn’t
             focus on mock-
             driven TDD.
Resiliant Against Refactoring
class Awesome
  def hello
    puts quot;~ Hello ~quot;
  end
end

describe Awesome do
  it quot;prints helloquot; do
    awesome = Awesome.new
    awesome.should_receive(:puts)
    awesome.hello
  end
end
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot;
  end
end

describe Awesome do
  it quot;prints helloquot; do
    awesome = Awesome.new
    awesome.should_receive(:puts)
    awesome.hello
  end
end
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot;
  end
end

describe Awesome do
  it quot;prints helloquot; do

              AIL
    awesome = Awesome.new
             F
    awesome.should_receive(:puts)
    awesome.hello
  end
end
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot;
  end
end

describe Awesome do
  it quot;prints helloquot; do
    capture { Awesome.new.hello }.
      should =~ /Hello/
  end
end
How to Test   Three Rules
Broken Interface means failing tests
Working Interface means passing tests
Write tests about what you care about
http://example.com/foo




        Filters




      Controller




        Views




       Helpers




         What Happens in Your App
http://example.com/foo




      Your App




                         What You Care About
http://example.com/foo




            4 Filters




            Controller




H   Views                Partials




             Helpers




                                    Refactoring
Iteration 1
class Foo < Application
  def awesome
    awesome_string
  end

  def awesome_string
    quot;Awesomequot;
  end
end

Merb::Router.prepare do
  match(quot;/foo/awesomequot;).
    to(:controller => Foo,
      :action => :awesome).
    name(:awesome)
end
dispatch_to(Foo, :awesome) do |cont|
  cont.should_receive(:awesome_string)
end
dispatch_to(Foo, :awesome) do |cont|
  cont.should_receive(:awesome_string)
end
Iteration 2
class Bar < Application
  def coolness
    render
  end
end

Merb::Router.prepare do
  match(quot;/foo/awesomequot;).
    to(:controller => Bar,
      :action => :coolness).
    name(:awesome)
end
dispatch_to(Foo, :awesome) do |cont|
  cont.should_receive(:awesome_string)
end
dispatch_to(Foo, :awesome) do |cont|
              AIL
  cont.should_receive(:awesome_string)
end
             F
request(quot;/foo/awesomequot;).body.
  should =~ /Awesome/
request(url(:awesome)).body.
  should =~ /Awesome/
Broken Interface means failing tests
Working Interface means passing tests
Write tests about what you care about
“It’s Too Hard”
Let’s Make it Easy
request(url(:speakers))

request(resource(@speaker))

request(quot;/foo/speakersquot;)
request(url(:speakers))

              one method
request(resource(@speaker))

request(quot;/foo/speakersquot;)
Sessions are automatically sticky in a spec
it quot;should let you inquot; do
  request(quot;/loginquot;, :method => :post,
    :params => {:username => quot;userquot;,
                :password => quot;passquot;})

  request(quot;/homequot;).should be_successful
end
it quot;should let you inquot; do
  request(quot;/loginquot;, :method => :post,
    :params => {:username => quot;userquot;,
                :password => quot;passquot;})

  request(quot;/homequot;).should be_successful
                       login
end
it quot;should let you inquot; do
  request(quot;/loginquot;, :method => :post,
    :params => {:username => quot;userquot;,
                :password => quot;passquot;})

  request(quot;/homequot;).should be_successful
end


                you’re logged in
describe quot;/loginquot;, :given => quot;successful loginquot; do
  it quot;should let you inquot; do
    request(quot;/homequot;).should be_successful
  end
end

                         simpler
describe quot;/loginquot;, :given => quot;successful loginquot; do
  it quot;should let you inquot; do
    request(quot;/homequot;).should be_successful
  end
end

                         simpler
Request




          Rack




status    body     headers
request() helper




              Rack




status        body          headers
request(quot;/fooquot;).body.should == quot;helloquot;

request(quot;/fooquot;).should have_xpath(quot;//h1quot;)

request(quot;/fooquot;).should(
  have_selector(quot;h1:contains(text)quot;))
request(quot;/fooquot;).body.should == quot;helloquot;

request(quot;/fooquot;).should have_xpath(quot;//h1quot;)

request(quot;/fooquot;).should(
  have_selector(quot;h1:contains(text)quot;))



              1.0 final
request(quot;/fooquot;).should be_client_error

request(quot;/fooquot;).should(
  have_content_type(:json))
Thank you.

More Related Content

Similar to Testing Merb

Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
Wen-Tien Chang
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
webhostingguy
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
Abhay Sapru
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 

Similar to Testing Merb (20)

Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
My First Rails Plugin - Usertext
My First Rails Plugin - UsertextMy First Rails Plugin - Usertext
My First Rails Plugin - Usertext
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de Rails
 
Php
PhpPhp
Php
 
Drupal site translation and translation testing
Drupal site translation and translation testingDrupal site translation and translation testing
Drupal site translation and translation testing
 
Cooking with Chef
Cooking with ChefCooking with Chef
Cooking with Chef
 
And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack Support
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
 
JavaScript Needn't Hurt!
JavaScript Needn't Hurt!JavaScript Needn't Hurt!
JavaScript Needn't Hurt!
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
DC |> Elixir Meetup - Going off the Rails into Elixir - Dan Ivovich
DC |> Elixir Meetup - Going off the Rails into Elixir - Dan IvovichDC |> Elixir Meetup - Going off the Rails into Elixir - Dan Ivovich
DC |> Elixir Meetup - Going off the Rails into Elixir - Dan Ivovich
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Sinatra
SinatraSinatra
Sinatra
 

More from Yehuda Katz

Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
Yehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OO
Yehuda Katz
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Making your oss project more like rails
Making your oss project more like railsMaking your oss project more like rails
Making your oss project more like rails
Yehuda Katz
 

More from Yehuda Katz (12)

Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 
Writing Fast Client-Side Code: Lessons Learned from SproutCore
Writing Fast Client-Side Code: Lessons Learned from SproutCoreWriting Fast Client-Side Code: Lessons Learned from SproutCore
Writing Fast Client-Side Code: Lessons Learned from SproutCore
 
SproutCore: Amber
SproutCore: AmberSproutCore: Amber
SproutCore: Amber
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OO
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Making your oss project more like rails
Making your oss project more like railsMaking your oss project more like rails
Making your oss project more like rails
 
Merb Camp Keynote
Merb Camp KeynoteMerb Camp Keynote
Merb Camp Keynote
 
Merb
MerbMerb
Merb
 
DataMapper
DataMapperDataMapper
DataMapper
 
jQuery and Ruby Web Frameworks
jQuery and Ruby Web FrameworksjQuery and Ruby Web Frameworks
jQuery and Ruby Web Frameworks
 
jQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersjQuery Presentation to Rails Developers
jQuery Presentation to Rails Developers
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Testing Merb