Save 37% off PRO during our Black Friday Sale! »

Outside in development with Cucumber and Rspec

3c28deaff162aeda44f2e0bcdca1dacf?s=47 Joseph Wilk
September 22, 2011

Outside in development with Cucumber and Rspec

Talk given by Joseph Wilk at Scotland on Rails 2009. Examines how Cucumber and Rspec fit together in the testing workflow, how and why Cucumber is useful and how it works. Walks through a simple example based on renting DVDs showing how to use Cucumber and some of the best practices. Looks at how Webrat can drive Selenium to in-turn drive Cucumber features through the browser.

3c28deaff162aeda44f2e0bcdca1dacf?s=128

Joseph Wilk

September 22, 2011
Tweet

Transcript

  1. utside in Development Cucumber Rspec Joseph Wilk / http://blog.josephwilk.net

  2. Why are you here?

  3. Why are you here? In order to ...

  4. Why are you here? In order to ... As a

    Scotland on Rails attendee
  5. Why are you here? In order to ... As a

    Scotland on Rails attendee I want ...
  6. What is your Acceptance Criteria?

  7. Scenario: Happy SoR attendees Given people turned up When Joseph

    talks Then everyone should acquire some new knowledge And no-one should fall asleep And no rotten food should be thrown
  8. Pass Fail

  9. Outside-in Models Views Controllers User Browser

  10. Cucumber Rspec “The feature you wished you had” “The components

    and the interactions you wish you had” Models Views Controllers
  11. Cucumber Rspec “The feature you wished you had” “The components

    and the interactions you wish you had” Models Views Controllers
  12. Cucumber Rspec “The feature you wished you had” “The components

    and the interactions you wish you had” Models Views Controllers
  13. Cucumber / Scenario

  14. Cucumber / Scenario

  15. Cucumber / Scenario Rspec / code examples

  16. Cucumber / Scenario Rspec / code examples

  17. Cucumber / Scenario Rspec / code examples

  18. Cucumber / Scenario Rspec / code examples

  19. Cucumber / Scenario Rspec / code examples

  20. :rspec => route finder :cucumber => destination London Edinburgh

  21. Behaviour Driven Development with elegance and joy

  22. Behaviour Driven Development with elegance and joy Just Cuke it!

  23. Plaintext Features Ruby step definitions

  24. Cucumber Feature Feature: Be awesome Narrative Scenario: title Given <some

    context> And <yet more context> When <some action> And <more actions> Then <some outcome> And <more outcomes> Not executed Example of behaviour
  25. Plaintext Ruby tests Cucumber Given /^we like (.*)$/i do |fruit|

    fruit.should == 'cukes' #assert_equal(fruit, 'cukes') end Given we like cukes Feature Step definition regexp match
  26. Plug and Pray Rails Plaintext Ruby tests Merb Sinatra Rspec

    TestUnit iPhone Cucumber Ruby-Gnome2 Cucumber
  27. Cucumber English

  28. concombre Ωϡ΢Ϧ Gurke Cucumber pepino cetriolo agurk ﺭﺎﻴﳋﺍ ketimun Japanese

    German English Arabic Indonisean Italian Spanish Danish French огурец Russian Chinese ԫӝ য়੉ Korean gurka Swedish Romanian castravete ogórek Polish
  29. concombre Ωϡ΢Ϧ Gurke Cucumber pepino cetriolo agurk ﺭﺎﻴﳋﺍ ketimun Japanese

    German English Arabic Indonisean Italian Spanish Danish French огурец Russian Chinese ԫӝ য়੉ Korean gurka Swedish Romanian castravete ogórek Polish Treetop Rocks ANTLR could rock harder
  30. Webrat Love visit home_path click_link "Web" fill_in "Email", :with =>

    "Rat” click_button "Love" Bansky
  31. Return On Investment Cucumber Feature

  32. Return On Investment Cucumber Feature • Token Conversation

  33. Return On Investment Cucumber Feature • Token Conversation • Acceptance

    Criteria
  34. Return On Investment Cucumber Feature • Token Conversation • Acceptance

    Criteria • Design
  35. Return On Investment Cucumber Feature • Token Conversation • Acceptance

    Criteria • Design • Documentation
  36. Return On Investment Cucumber Feature • Token Conversation • Acceptance

    Criteria • Design • Documentation • Functional test
  37. Return On Investment Cucumber Feature • Token Conversation • Acceptance

    Criteria • Design • Documentation • Functional test • Integration test
  38. Token for Conversation

  39. None
  40. Feature Request I want members to be able to rent

    a movie with a priority indicating how much they want to see the film
  41. Show me the _ _ _ _ _

  42. Show me the _ _ _ _ _ VA LU

    E
  43. Root Cause Analysis

  44. So we can maximise allocation of films Why? Root Cause

    Analysis
  45. Why? So we can maximise allocation of films Why? Keep

    customers happy Root Cause Analysis
  46. Why? Why? So we can maximise allocation of films Why?

    Keep customers happy So they continue with their subscriptions Root Cause Analysis
  47. Why? Why? So we can maximise allocation of films Why?

    Keep customers happy So they continue with their subscriptions Root Cause Analysis
  48. Lets write our Cucumber feature

  49. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Role Value Feature
  50. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Narrative Role Value Feature
  51. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Role Value Feature
  52. Feature (terrible) Feature: select a movie to rent with priority

    In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority
  53. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Noise
  54. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Too generic role
  55. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Too generic role Film member
  56. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Role can change feature Film member
  57. Feature: select a movie to rent with priority In order

    to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Feature (terrible) Role can change feature Film Member selects a movie to rent with priority Film member
  58. Feature (terrible) Nice Feature: select a movie to rent with

    priority In order to maximise allocation of films The website user Needs to be able to add movies to their rental list with a priority Film Member selects a movie to rent with priority Film member
  59. Acceptance Definition of Done.

  60. Scenarios Feature: Film Member selects a movie to rent with

    priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority
  61. Scenarios Feature: Film Member selects a movie to rent with

    priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority Then I should see "My rental list" And I should see "Casshern" in my rental list And "Casshern" should be marked as "High priority"
  62. Scenarios Feature: Film Member selects a movie to rent with

    priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority Then I should see "My rental list" And I should see "Casshern" in my rental list And "Casshern" should be marked as "High priority" When I choose "High priority" And I press "Rent"
  63. Scenarios Feature: Film Member selects a movie to rent with

    priority In order to maximise allocation of films The Film member Needs to add movies to their rental list with a priority Scenario: High priority Then I should see "My rental list" And I should see "Casshern" in my rental list And "Casshern" should be marked as "High priority" When I choose "High priority" And I press "Rent" Given I'm logged in And I am viewing the movie "Casshern"
  64. None
  65. Let there be Cuking

  66. Demo High Resolution: http://www.screencast.com/t/aG1sE4N3Zq Low Resolution: http://www.vimeo.com/3910617

  67. Here’s one I Cuked earlier

  68. Demo High Resolution: http://www.screencast.com/t/tlBYxkKci Low Resolution http://www.vimeo.com/3911104

  69. Installing Cucumber $ sudo gem install cucumber $ script/plugin install

    git://github.com/aslakhellesoy/ cucumber.git $ gem sources -a http://gems.github.com $ sudo gem install aslakhellesoy-cucumber
  70. Fixtures Scenario: search results found Given the Movies | title

    | | The colour white | | The colour red | | Casshern | When I search for "colour" Then I will see 2 results Scenario: search results found Given 2 films with "colour" in the title When I search for "colour" Then I will see 2 results Given /^the Movies$/i do |movie_hash| #{:title => "The colour white"}, #{:title => "The colour red"}, #{:title => "Casshern"} movies.hashes.each do |values| Movie.create!(values) end end
  71. Stick it to the World module NavigationHelpers def path_to(page_name) case

    page_name when /the homepage/ root_path else raise "Can't find mapping from \"#{page_name}\" to a path." end end end World do |world| world.extend NavigationHelpers world end
  72. Hooks Hooks are GLOBAL! Before do # do something before

    each scenario. end After do #do something after each scenario end
  73. How many scenarios is enough? • Given/When/Then “Finite state machine”

    Uncle Bob • What’s the Return on Investment?
  74. Cowcumbers (Bad smells) “fit only for consumption by cows”

  75. Given /^dirty state$/ do @movie = Movie.create! end Given /^coupled

    by state$/ do @movie.title = "yucky" end Cowcumbers Tests without user value Relying on state (too much) Given /^does a user care$/ do response.current_url == ‘silly’ end Given /^checking the db$/ do Movie.find(1).should_not == nil end
  76. Building your DSL

  77. Concrete vs Abstract Scenarios Concrete Abstract Domain understanding Given I

    go to the login page And I fill in "username" with "joseph" And I fill in "password" with "cuker" And I click "login" Given I'm logged in
  78. Calling steps from steps Given /I’m logged in/ do User.create!(:user

    => 'josephwilk', :password => "pass") Given 'I fill in "password" with "josephwilk"' Given 'I fill in "password" with "pass"' Given 'I click "login"' end
  79. Feature Request Can you add some of that funky AJAX

    stuff for the rent button
  80. JavaScript kills Webrat

  81. Watir, Selenium & Celerity Browser based • Watir • Selenium

    Headless Browser • Celerity • Culerity - http:// github.com/langalex/ culerity/tree/master
  82. Webrat / Selenium # Sets up the Rails environment for

    Cucumber ENV["RAILS_ENV"] ||= "selenium" require File.expand_path(File.dirname(__FILE__) + '/../../config/environment') require 'cucumber/rails/world' require 'cucumber/formatters/unicode' #Cucumber::Rails.use_transactional_fixtures require 'webrat' Webrat.configure do |config| config.mode = :selenium end require 'cucumber/rails/rspec' require 'webrat/core/matchers' Before do Movie.delete_all RentalRequest.delete_all end Turn off Switch to Selenium Manually cleanup
  83. Organising cucumber --profile rails --tags ~@js cucumber --profile browser --tags

    @js @in-progress Feature: Verify billing @js Scenario: Missing product description Scenario: Several products Tag
  84. Demo High Resolution: http://www.screencast.com/t/1jRSI6TvXWu Low Resolution: http://www.vimeo.com/3911241

  85. Further reading • http://cukes.info • http://wiki.github.com/ aslakhellesoy/cucumber • http://rspec.info/ •

    http://blog.josephwilk.net
  86. Thanks joe@josephwilk.net http://github.com/josephwilk