5067 comments
2220 subscribers
4798 on Twitter
Subscribe! Feed reader E-mail

Cucumber, Capybara, and the joys of integration testing in Rails

Development is so much more fun with test cases. They give you a big target to aim for, and it feels fantastic when you write the code to make them pass. Tests also avoid or shorten those late-night “oh no! I broke something!” sessions, because you can backtrack to versions that pass the tests. (You are using version control, right?)

So naturally, as I worked on my first IBM project using Ruby on Rails, I wanted to know about how to perform automated testing – not just at the unit level, but at the web/integration level.

I like using Simpletest in Drupal. I love the testing frameworks available in Rails.

You see, Cucumber for Rails allows you to write your tests in English (or something reasonably close to it). For example:

Feature: Contributor
  In order to maintain security
  As a contributor
  I want to be able to edit existing submissions
  Scenario: Contributor should not be able to create or delete submissions
    Given I am a company contributor
    And there is a 2010 survey for "Company X"
    When I view the dashboard
    Then I should not be able to delete a submission
    And I should not be able to create a submission

Putting that in my features/contributor.feature" file and executing that with =bundle execute cucumber features/contributor.feature gets me a lovely test with green signs all around.

You’re thinking: Rails is awesome, but it’s not that awesome, is it? How can it know about the specifics of the application?

Rails knows because I’ve written my own step definitions for Cucumber. Step definitions are simple. You can define them with a regular expression like this:

When /^I view the dashboard/ do
  visit root_path
end

Then /^I should not be able to create a submission/ do
  page.should_not have_button("Create submission")
end

You can also define steps that parse arguments from the string or call other steps:

Given /^there is a ([^ ]+) survey for \"([^\"]+)\"$/ do |year,name|
  @company = Company.find_by_name(name)
  assert !@company.nil?
  Given "there is a #{year} survey"
end

You can even take multi-line input, such as tables.

Automated testing is so awesome!

Short URL: http://sachachua.com/blog/p/22220

One Response to “Cucumber, Capybara, and the joys of integration testing in Rails”

  1. *like* – I usually tend to document my tests as asserts – so close to the code. But I can see the value in this when communicating with folk who are not as passionate about automated testing.

Comment, share a thought, ask a question...

Please comment as you, not your organization.





 

On This Day...

  • 2012: Sketchnotes: Designing content so that it works – Carl Friesen (#torontob2b) — Designing Content So It Works Carl Friesen, Global Reach Communications Like these? Check out my other sketchnotes, visual book notes/reviews, and visual [...]
  • 2012: Sketchnotes: Building a Social Enterprise – Andrew Jenkins (#torontob2b) — Click on the images to view larger versions. I might redraw these sometime – I still have to get the [...]
  • 2012: Sketchnotes: Marketing Automation, Jeffrey Yee (#torontob2b) — Marketing Automation Jeffrey Yee, Eloqua Like these? Check out my other sketchnotes, visual book notes/reviews, and visual metaphors. Here’s the text from the [...]
  • 2012: Optimism, happiness, and being young — Someone remarked that I’m clearly an optimistic person, and asked me how old I was. When I told him that [...]
  • 2010: Bread of salt and taste of home — Pandesal. Brown paper bags of crunchy-yet-soft buns at breakfast and merienda, often accompanied by hot chocolate—or chocolate porridge, if I [...]
  • 2007: Songs within us — W seemed surprised when I readily agreed to go to the summer concert at J’s elementary school. After all, I had [...]
  • 2007: The end is near! — I did my first two usability tests today. Yay! The end is in sight! Thanks to Stephen and Pierre for being [...]
  • 2006: Congrats to Diane on her game design class! — Diane Lazaro’s just learned that she’s going to teach a game design class. I’m sure she’ll do a fantastic job! =) Random [...]
  • 2006: Entrepreneurship — I really enjoyed talking to Winston Damarillo about the Philippines, and I’m looking forward to introducing him to other people who [...]
  • 2005: Wheee… =) — did you catch my message on livejournal? :D Hmmm, gotta check... Which entry? i was asking if i could [...]
  • 2005: LIES, DAMNED LIES AND STATISTICS — 39 words The mayor raised his hands triumphantly. “According to a survey by the Asian Development Bank, the Philippines is less corrupt [...]
  • 2004: Tweaked yahoogroups_files.pl — Downloaded yahoogroups_files.pl from http://grabyahoogroup.sourceforge.net . It’s a Yahoo Group archiver and perfect for my needs, although I needed to tweak the Perl [...]