Skip to content

Instantly share code, notes, and snippets.

@shmdt
Forked from stepheneyer/cucumber-rails.md
Created January 31, 2019 09:39
Show Gist options
  • Save shmdt/6fff8e4e63dcaf80be0f4533d3538e85 to your computer and use it in GitHub Desktop.
Save shmdt/6fff8e4e63dcaf80be0f4533d3538e85 to your computer and use it in GitHub Desktop.

Revisions

  1. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@ Given(/^I am on the home page$/) do
    pending # express the regexp above with the code you wish you had
    end
    Then(/^I should see "(.*?)"\.$/) do |arg1|
    Then(/^I should see "(.*?)"$/) do |arg1|
    pending # express the regexp above with the code you wish you had
    end
    ```
    @@ -108,7 +108,7 @@ Given(/^I am on the home page$/) do
    visit "/"
    end
    Then(/^I should see "(.*?)"\.$/) do |text|
    Then(/^I should see "(.*?)"$/) do |text|
    page.has_content?(text)
    end
    ```
  2. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@ cucumber features/welcome.feature
    You'll see that your tests needs a step_definition.

    ```
    Given(/^I am on the welcome page$/) do
    Given(/^I am on the home page$/) do
    pending # express the regexp above with the code you wish you had
    end
    @@ -104,7 +104,7 @@ end
    Create a "welcome_steps.rb" file inside the "step_definitions" subfolder in the "features" folder. Change the test code using the [Capybara](http://www.rubydoc.info/github/jnicklas/capybara/master) syntax to something like this:

    ```
    Given(/^I am on the welcome page$/) do
    Given(/^I am on the home page$/) do
    visit "/"
    end
  3. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -109,7 +109,7 @@ Given(/^I am on the welcome page$/) do
    end
    Then(/^I should see "(.*?)"\.$/) do |text|
    page.has_content?(text)
    page.has_content?(text)
    end
    ```
    Like all good tests, it should fail the first time you run it.
  4. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ Install Cucumber, which will create a folder in your project called "features" w
    ```
    rails g cucumber:install
    ```
    Create a test file in the "features" folder with a .feature extension. Use can use the example from above.
    Create a test file called "welcome.feature" in the "features" folder. Make sure to use the .feature extension. Use can use the example from above.

    ```
    Feature: Welcome
  5. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    ![](http://i.giphy.com/TJry0ut9mo6pq.gif)
    ![](http://i.giphy.com/Q7fa42S3mYLaU.gif)

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  6. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    ![](http://giphy.com/gifs/TJry0ut9mo6pq/html5)
    ![](http://i.giphy.com/TJry0ut9mo6pq.gif)

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  7. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    ![](http://gph.is/14W0v1T)
    ![](http://giphy.com/gifs/TJry0ut9mo6pq/html5)

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  8. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    ![](http://i.imgur.com/60bts.gif)
    ![](http://gph.is/14W0v1T)

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  9. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    <div style="max-width: 500px;" id="_giphy_TJry0ut9mo6pq"></div><script>var _giphy = _giphy || []; _giphy.push({id: "TJry0ut9mo6pq",w: 245, h: 138});var g = document.createElement("script"); g.type = "text/javascript"; g.async = true;g.src = ("https:" == document.location.protocol ? "https://" : "http://") + "giphy.com/static/js/widgets/embed.js";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(g, s);</script>
    ![](http://i.imgur.com/60bts.gif)

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  10. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Cucumber allows software developers to describe how software should behave in pl

    Capybara is the largest rodent known to man.

    http://giphy.com/gifs/TJry0ut9mo6pq/html5
    <div style="max-width: 500px;" id="_giphy_TJry0ut9mo6pq"></div><script>var _giphy = _giphy || []; _giphy.push({id: "TJry0ut9mo6pq",w: 245, h: 138});var g = document.createElement("script"); g.type = "text/javascript"; g.async = true;g.src = ("https:" == document.location.protocol ? "https://" : "http://") + "giphy.com/static/js/widgets/embed.js";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(g, s);</script>

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

  11. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,11 @@

    Cucumber allows software developers to describe how software should behave in plain text. The text is written in a business-readable, domain-specific language. This allows non-programmers to write specific feature requests that can be turned into automated tests that drive development of the project.

    Capybara is the largest rodent known to man. It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.
    Capybara is the largest rodent known to man.

    <iframe src="//giphy.com/embed/TJry0ut9mo6pq" width="480" height="270" frameBorder="0" style="max-width: 100%" class="giphy-embed" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    http://giphy.com/gifs/TJry0ut9mo6pq/html5

    It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

    ##Why is this technology useful?
    Cucumber allows tests to be more readable because they are written in what looks like plain English, although it is actualy [Gherkin](https://github.com/cucumber/cucumber/wiki/Gherkin). Behavior-driven development (BDD) is partly about understanding what the client wants before you begin coding, so tests are written with plain text which makes it easier to read and understand the feature requests you'll be coding from the start. Each Cucumber test is written in a story format that includes a feature request and matching scenario.
  12. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,9 @@

    Cucumber allows software developers to describe how software should behave in plain text. The text is written in a business-readable, domain-specific language. This allows non-programmers to write specific feature requests that can be turned into automated tests that drive development of the project.

    Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.
    Capybara is the largest rodent known to man. It is also software that helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

    <iframe src="//giphy.com/embed/TJry0ut9mo6pq" width="480" height="270" frameBorder="0" style="max-width: 100%" class="giphy-embed" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>

    ##Why is this technology useful?
    Cucumber allows tests to be more readable because they are written in what looks like plain English, although it is actualy [Gherkin](https://github.com/cucumber/cucumber/wiki/Gherkin). Behavior-driven development (BDD) is partly about understanding what the client wants before you begin coding, so tests are written with plain text which makes it easier to read and understand the feature requests you'll be coding from the start. Each Cucumber test is written in a story format that includes a feature request and matching scenario.
  13. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    #Cucumber - Behavior Driven Development
    #Cucumber and Capybara - Behavior Driven Development

    ##Overview

    Cucumber allows software developers to describe how software should behave in plain text. The text is written in a business-readable, domain-specific language. This allows non-programmers to write specific feature requests that can be turned into automated tests that drive development of the project.

    Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and is built into Rails applications.

    ##Why is this technology useful?
    Cucumber allows tests to be more readable because they are written in what looks like plain English, although it is actualy [Gherkin](https://github.com/cucumber/cucumber/wiki/Gherkin). Behavior-driven development (BDD) is partly about understanding what the client wants before you begin coding, so tests are written with plain text which makes it easier to read and understand the feature requests you'll be coding from the start. Each Cucumber test is written in a story format that includes a feature request and matching scenario.

    @@ -95,7 +97,7 @@ Then(/^I should see "(.*?)"\.$/) do |arg1|
    end
    ```

    Create a "welcome_steps.rb" file inside the "step_definitions" subfolder in the "features" folder. Change the test code to this:
    Create a "welcome_steps.rb" file inside the "step_definitions" subfolder in the "features" folder. Change the test code using the [Capybara](http://www.rubydoc.info/github/jnicklas/capybara/master) syntax to something like this:

    ```
    Given(/^I am on the welcome page$/) do
  14. @stepheneyer stepheneyer revised this gist Jul 8, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cucumber-rails.md
    Original file line number Diff line number Diff line change
    @@ -119,7 +119,7 @@ Run your test again. Does it pass? Once all your tests are green like a cucumber
    The process of testing is not simple, no matter what tool you use. Although users are able to write Cucumber tests in plain text, it still requires development time to write the code to pass the tests. This can be as challenging or even more challening then just coding it upfront.

    ###Not familiar with DSL testing language
    It wasn't initally very clear how to write the test code needed pass the test. The documentation and tutorials for Cucumber are not great on their site. They would benefit from a basic step-by-step tutorial that walks you through exactly how to create your first test and better examples of the language used for Cucumber vs. Capybara.
    It wasn't initally very clear how to write the test code needed pass the test. The documentation and tutorials for Cucumber are not great on their site. They would benefit from a basic step-by-step tutorial that walks you through exactly how to create your first test.

    Of course, the best place to look is the docs. I included some resources below that might be helpful.

    @@ -139,7 +139,7 @@ Sometimes it can be overwhelming to think about all the tests you might need to

    [Cucumber](https://cucumber.io/) - Simple, human collaboration

    [Capybara](http://www.rubydoc.info/github/jnicklas/capybara/master) - Ruby docs including the domain-specific language(DSL)
    [Capybara](http://www.rubydoc.info/github/jnicklas/capybara/master) - Ruby docs with coding syntax

    [Business-Readable DSL](http://www.martinfowler.com/bliki/BusinessReadableDSL.html) - Martin Fowler's explanation

  15. @stepheneyer stepheneyer renamed this gist Jul 8, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. @stepheneyer stepheneyer renamed this gist Jul 8, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. @stepheneyer stepheneyer created this gist Jul 8, 2015.
    149 changes: 149 additions & 0 deletions cucumber rails
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    #Cucumber - Behavior Driven Development

    ##Overview

    Cucumber allows software developers to describe how software should behave in plain text. The text is written in a business-readable, domain-specific language. This allows non-programmers to write specific feature requests that can be turned into automated tests that drive development of the project.

    ##Why is this technology useful?
    Cucumber allows tests to be more readable because they are written in what looks like plain English, although it is actualy [Gherkin](https://github.com/cucumber/cucumber/wiki/Gherkin). Behavior-driven development (BDD) is partly about understanding what the client wants before you begin coding, so tests are written with plain text which makes it easier to read and understand the feature requests you'll be coding from the start. Each Cucumber test is written in a story format that includes a feature request and matching scenario.

    ###For example:

    ```
    Feature: Welcome
    In order to read the page
    As a viewer
    I want to see the home page of my app

    Scenario: View welcome page
    Given I am on the home page
    Then I should see "Welcome to my site."
    ```

    Each scenario can have up to three parts: Givens, Whens, and Thens:

    **Given**: Given lines describe what pre-condition should exist.

    **When**: When lines describe the actions you take.

    **Then**: Then lines describe the result.

    You can also add **And** lines, which do whatever the line above them does.




    ###Steps to implement Cucumber in Ruby on Rails:

    Create a new Ruby on Rails project.

    ```
    rails new cucumber -d postgresql
    ```
    ```
    cd cucumber
    ```
    ```
    rake db: create
    ```
    Install the Cucumber-Rails gem in your project's Gemfile.

    ```
    group :test, :development do
    gem 'cucumber-rails', :require => false
    # database_cleaner is not required, but highly recommended
    gem 'database_cleaner'
    end
    ```
    Remember to bundle install and then restart your Rails server.

    ```
    bundle install
    ```
    Install Cucumber, which will create a folder in your project called "features" with a sub-folder called "step_definitions"

    ```
    rails g cucumber:install
    ```
    Create a test file in the "features" folder with a .feature extension. Use can use the example from above.

    ```
    Feature: Welcome
    In order to read the page
    As a viewer
    I want to see the home page of my app

    Scenario: View welcome page
    Given I am on the home page
    Then I should see "Welcome to my site."
    ```
    Then run your test.

    ```
    cucumber features/welcome.feature
    ```

    You'll see that your tests needs a step_definition.

    ```
    Given(/^I am on the welcome page$/) do
    pending # express the regexp above with the code you wish you had
    end

    Then(/^I should see "(.*?)"\.$/) do |arg1|
    pending # express the regexp above with the code you wish you had
    end
    ```

    Create a "welcome_steps.rb" file inside the "step_definitions" subfolder in the "features" folder. Change the test code to this:

    ```
    Given(/^I am on the welcome page$/) do
    visit "/"
    end

    Then(/^I should see "(.*?)"\.$/) do |text|
    page.has_content?(text)
    end
    ```
    Like all good tests, it should fail the first time you run it.
    Make changes to your code so it will pass the test.

    **Hint**: Create a Welcome controller, assign the root route to welcome#index, and create the index view with the content 'Welcome to my site'.

    Run your test again. Does it pass? Once all your tests are green like a cucumber, you're good to go!

    ##What were the roadblocks?

    ###Testing is not easy
    The process of testing is not simple, no matter what tool you use. Although users are able to write Cucumber tests in plain text, it still requires development time to write the code to pass the tests. This can be as challenging or even more challening then just coding it upfront.

    ###Not familiar with DSL testing language
    It wasn't initally very clear how to write the test code needed pass the test. The documentation and tutorials for Cucumber are not great on their site. They would benefit from a basic step-by-step tutorial that walks you through exactly how to create your first test and better examples of the language used for Cucumber vs. Capybara.

    Of course, the best place to look is the docs. I included some resources below that might be helpful.


    ##What are the takeaways and lessons learned?

    ###The sexy thing to do is not necessarily the best thing
    Whle most developers know they should use BDD, it's never quite as fun or as sexy as just diving into the code. Making the time to do BDD when working on a project is like telling someone they need to skip dessert and eat broccoli instead. Only the most disciplined among us are likely to actually do it.

    ###Code first, then test?
    One recent suggestion I heard recently is that it might be more practical for developers and startups to code the first itiration of their idea, determine they have a good product-market fit and are going to keep the code they created (and are not going to toss it), and then write tests to make sure they have critical parts of their project protected from future refactoring, added features, etc.

    ###Get one test working first
    Sometimes it can be overwhelming to think about all the tests you might need to write for your project. By getting one test written and passing, it can provide the needed extra motivation to make more of them. Learning the syntax needed to pass one test helps provide the confidence to write and pass more of them.

    ##Resources

    [Cucumber](https://cucumber.io/) - Simple, human collaboration

    [Capybara](http://www.rubydoc.info/github/jnicklas/capybara/master) - Ruby docs including the domain-specific language(DSL)

    [Business-Readable DSL](http://www.martinfowler.com/bliki/BusinessReadableDSL.html) - Martin Fowler's explanation

    [Ruby for Newbies: Testing Web Apps with Capybara and Cucumber](http://code.tutsplus.com/tutorials/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber--net-21446) - Tuts+ tutorial for Cucumber and Rails

    [Velocity](http://velocity.meteor.com/) - Meteor's testing application that uses Cucumber