• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Behaviour Driven Development with Cucumber, RSpec, and Shoulda

  • 3,457 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
3,457
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
89
Comments
1
Likes
4

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. Behaviour Driven Development with Cucumber, RSpec, and Shoulda
  • 2. What is BDD?
  • 3. What is BDD? Agile Test-driven Outside-in Based on the user’s experience Natural language
  • 4. Why test?
  • 5. Why test? “The bigger they come, the harder they fall.” Easier to debug Better understanding of requirements More flexible, reusable code "A bug is not an error in logic, it is a test you forgot to write."
  • 6. Outside-in
  • 7. Outside-in Satisfy the needs of stakeholders Speak in natural language Test in natural language Red-Green-Refactor*
  • 8. Outside-in Satisfy the needs of stakeholders Speak in natural language Test in natural language Red-Green-Refactor* *We’ll come back to this later
  • 9. The Perfect Suite
  • 10. The Perfect Suite Cucumber BDD Acceptance Testing Framework RSpec BDD Unit Testing Framework Shoulda Test macros built upon Test::Unit
  • 11. Cucumber
  • 12. Cucumber Uses plain text to convey test cases Speaks to stakeholder needs Addresses application behaviour from the user’s POV
  • 13. Cucumber Uses plain text to convey test cases Speaks to stakeholder needs Addresses application behaviour from the user’s POV Enough talk, let’s see some code!
  • 14. Cucumber cont’d
  • 15. Cucumber cont’d Typical Feature Description
  • 16. Cucumber cont’d Typical Feature Description In order to <gain business value> As a <role> I want to <perform an action>
  • 17. Cucumber cont’d Typical Feature Description In order to <gain business value> As a <role> I want to <perform an action> Typical Test Scenario
  • 18. Cucumber cont’d Typical Feature Description In order to <gain business value> As a <role> I want to <perform an action> Typical Test Scenario Given <some context> When I <perform some action> Then I should <observe expected outcome>
  • 19. Cucumber in the real world
  • 20. Cucumber in the real world One feature summary per file
  • 21. Cucumber in the real world One feature summary per file <Role> <performs action>
  • 22. Cucumber in the real world One feature summary per file <Role> <performs action> Feature: User submits blog post In order to share my thoughts with the world As a blog author I want to post to my blog
  • 23. Cucumber in the real world One feature summary per file <Role> <performs action> Feature: User submits blog post In order to share my thoughts with the world As a blog author I want to post to my blog
  • 24. Cucumber in the real world One feature summary per file <Role> <performs action> Feature: User submits blog post In order to share my thoughts with the world As a blog author I want to post to my blog
  • 25. Cucumber in the real world
  • 26. Cucumber in the real world Multiple scenarios (test cases) per feature Exercise entire application stack Write from stakeholders’ perspective
  • 27. Cucumber in the real world Multiple scenarios (test cases) per feature Exercise entire application stack Write from stakeholders’ perspective Scenario: Submit valid blog post Given I am on the new post page When I fill in “title” with “My Cat Miffles” And I fill in “body” with “Argle bargle.” And I press “submit” Then I should be on the blog page And I should see “My Cat Miffles”
  • 28. Cucumber Step Definitions
  • 29. Cucumber Step Definitions Maps test code to plain-text steps
  • 30. Cucumber Step Definitions Maps test code to plain-text steps Given I am on the new post page
  • 31. Cucumber Step Definitions Maps test code to plain-text steps Given I am on the new post page Given /I am on (.+)$/ do |page_name|   visit path_to(page_name) end
  • 32. Cucumber Step Definitions Maps test code to plain-text steps Given I am on the new post page Given /I am on (.+)$/ do |page_name|   visit path_to(page_name) end
  • 33. Cucumber Step Definitions Maps test code to plain-text steps Given I am on the new post page Given /I am on (.+)$/ do |page_name|   visit path_to(page_name) end
  • 34. Cucumber Step Definitions Maps test code to plain-text steps Given I am on the new post page Given /I am on (.+)$/ do |page_name|   visit path_to(page_name) end
  • 35. RSpec
  • 36. RSpec Unit tests in plain english Integrated mocking and stubbing framework Plays nice with Test::Unit
  • 37. RSpec By Example
  • 38. RSpec By Example describe Calculator do it “performs addition” do calc = Calulator.new sum = calc.add(2, 2) sum.should eq(4) end end
  • 39. RSpec By Example describe Calculator do it “performs addition” do calc = Calulator.new sum = calc.add(2, 2) sum.should eq(4) end end Beautiful!
  • 40. Shoulda
  • 41. Shoulda Built in matchers and macros to make writing tests easier Uses common Rails testing conventions and idioms Easy to extend and build custom macros to further ease testing Plays nice with Test::Unit & RSpec
  • 42. Shoulda & RSpec
  • 43. Shoulda & RSpec Rails Model Tests
  • 44. Shoulda & RSpec Rails Model Tests describe Post do it { should validate_presence_of :body } it { should validate_uniqueness_of :title } end
  • 45. Shoulda & RSpec Rails Model Tests describe Post do it { should validate_presence_of :body } it { should validate_uniqueness_of :title } end Rails Controller Tests
  • 46. Shoulda & RSpec Rails Model Tests describe Post do it { should validate_presence_of :body } it { should validate_uniqueness_of :title } end Rails Controller Tests describe PostsController do it { should route(:get => ‘posts’).to(:action => ‘index’) } it { should set_the_flash.to(“Congratulations!”) it { should redirect_to blog_path } end
  • 47. Putting It All Together
  • 48. Putting It All Together Coming back to “Red-Green-Refactor” The blog example (please save your groans for later) Rails 3 Ruby 1.8.7 Source available online http://github.com/drmanitoba/blog_example
  • 49. Red-Green-Refactor
  • 50. Red-Green-Refactor Get to Red
  • 51. Red-Green-Refactor Get to Red Write your test FIRST!
  • 52. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD
  • 53. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail
  • 54. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green
  • 55. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green Write your implementation code
  • 56. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green Write your implementation code Run the suite and watch it pass
  • 57. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green Write your implementation code Run the suite and watch it pass Refactor
  • 58. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green Write your implementation code Run the suite and watch it pass Refactor Look for opportunities to improve your code
  • 59. Red-Green-Refactor Get to Red Write your test FIRST! Use the code you WISH YOU HAD Run the suite and watch your test fail Get to Green Write your implementation code Run the suite and watch it pass Refactor Look for opportunities to improve your code Run the tests again, you should see still see green
  • 60. R-G-R in Practice
  • 61. R-G-R in Practice
  • 62. R-G-R in Practice From Cucumber Write your scenario Get to red Drill down into RSpec Red-Green- Refactor Back up to Cucumber Get to Green
  • 63. Some Helpful Links http://github.com/aslakhellesoy/cucumber http://github.com/rspec/rspec http://github.com/thoughtbot/shoulda http://www.pragprog.com/titles/achbd/the-rspec-book
  • 64. And now, code.