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
            ...
Resiliant Against Refactoring
class Awesome
  def hello
    puts quot;~ Hello ~quot;
  end
end

describe Awesome do
  it quot;prints helloquot; do
    a...
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot...
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot...
class Awesome
  def hello
    awesome_print quot;~ Hello ~quot;
  end

  def awesome_print(str)
    print quot;#{str}nquot...
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 Hap...
http://example.com/foo




      Your App




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




            4 Filters




            Controller




H   Views                Partials




    ...
Iteration 1
class Foo < Application
  def awesome
    awesome_string
  end

  def awesome_string
    quot;Awesomequot;
  end
end

Merb...
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;).
    ...
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...
it quot;should let you inquot; do
  request(quot;/loginquot;, :method => :post,
    :params => {:username => quot;userquot...
it quot;should let you inquot; do
  request(quot;/loginquot;, :method => :post,
    :params => {:username => quot;userquot...
describe quot;/loginquot;, :given => quot;successful loginquot; do
  it quot;should let you inquot; do
    request(quot;/h...
describe quot;/loginquot;, :given => quot;successful loginquot; do
  it quot;should let you inquot; do
    request(quot;/h...
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...
request(quot;/fooquot;).body.should == quot;helloquot;

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

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

request(quot;/fooquot;).should(
  have_content_type(:json))
Thank you.
Upcoming SlideShare
Loading in...5
×

Testing Merb

3,035

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

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

No notes for slide

Testing Merb

  1. 1. Testing Merb The Right Way
  2. 2. Why Test?
  3. 3. TDD
  4. 4. TDD and Regressions
  5. 5. TDD and Regressions I am not saying TDD is bad, but this talk doesn’t focus on mock- driven TDD.
  6. 6. Resiliant Against Refactoring
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. How to Test Three Rules
  12. 12. Broken Interface means failing tests
  13. 13. Working Interface means passing tests
  14. 14. Write tests about what you care about
  15. 15. http://example.com/foo Filters Controller Views Helpers What Happens in Your App
  16. 16. http://example.com/foo Your App What You Care About
  17. 17. http://example.com/foo 4 Filters Controller H Views Partials Helpers Refactoring
  18. 18. Iteration 1
  19. 19. 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
  20. 20. dispatch_to(Foo, :awesome) do |cont| cont.should_receive(:awesome_string) end
  21. 21. dispatch_to(Foo, :awesome) do |cont| cont.should_receive(:awesome_string) end
  22. 22. Iteration 2
  23. 23. class Bar < Application def coolness render end end Merb::Router.prepare do match(quot;/foo/awesomequot;). to(:controller => Bar, :action => :coolness). name(:awesome) end
  24. 24. dispatch_to(Foo, :awesome) do |cont| cont.should_receive(:awesome_string) end
  25. 25. dispatch_to(Foo, :awesome) do |cont| AIL cont.should_receive(:awesome_string) end F
  26. 26. request(quot;/foo/awesomequot;).body. should =~ /Awesome/
  27. 27. request(url(:awesome)).body. should =~ /Awesome/
  28. 28. Broken Interface means failing tests
  29. 29. Working Interface means passing tests
  30. 30. Write tests about what you care about
  31. 31. “It’s Too Hard”
  32. 32. Let’s Make it Easy
  33. 33. request(url(:speakers)) request(resource(@speaker)) request(quot;/foo/speakersquot;)
  34. 34. request(url(:speakers)) one method request(resource(@speaker)) request(quot;/foo/speakersquot;)
  35. 35. Sessions are automatically sticky in a spec
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. describe quot;/loginquot;, :given => quot;successful loginquot; do it quot;should let you inquot; do request(quot;/homequot;).should be_successful end end simpler
  40. 40. describe quot;/loginquot;, :given => quot;successful loginquot; do it quot;should let you inquot; do request(quot;/homequot;).should be_successful end end simpler
  41. 41. Request Rack status body headers
  42. 42. request() helper Rack status body headers
  43. 43. 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;))
  44. 44. 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
  45. 45. request(quot;/fooquot;).should be_client_error request(quot;/fooquot;).should( have_content_type(:json))
  46. 46. Thank you.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×