#155
Mar 30, 2009

Beginning with Cucumber

Cucumber is a high-level testing framework. In this episode we will create a new Rails application from scratch using behavior driven development.
Download (30.3 MB, 15:22)
alternative download for iPod & Apple TV (19.6 MB, 15:22)

Resources

rails blog
sudo rake gems:install RAILS_ENV=test
script/generate cucumber
cucumber features -n
script/generate rspec_model article title:string content:text
rake db:migrate
rake db:test:clone
script/generate rspec_controller articles index
Feature: Manage Articles
  In order to make a blog
  As an author
  I want to create and manage articles
  
  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"
  
  Scenario: Create Valid Article
    Given I have no articles
    And I am on the list of articles
    When I follow "New Article"
    And I fill in "Title" with "Spuds"
    And I fill in "Content" with "Delicious potato wedges!"
    And I press "Create"
    Then I should see "New article created."
    And I should see "Spuds"
    And I should see "Delicious potato wedges!"
    And I should have 1 article
# config/environments/test.rb
config.gem "rspec", :lib => false, :version => ">=1.2.2"
config.gem "rspec-rails", :lib => false, :version => ">=1.2.2"
config.gem "webrat", :lib => false, :version => ">=0.4.3"
config.gem "cucumber", :lib => false, :version => ">=0.2.2"

# features/step_definitions/article_steps.rb
Given /^I have articles titled (.+)$/ do |titles|
  titles.split(', ').each do |title|
    Article.create!(:title => title)
  end
end

Given /^I have no articles$/ do
  Article.delete_all
end

Then /^I should have ([0-9]+) articles?$/ do |count|
  Article.count.should == count.to_i
end

# articles_controller.rb
def index
  @articles = Article.all
end

def new
  @article = Article.new
end

def create
  @article = Article.create!(params[:article])
  flash[:notice] = "New article created."
  redirect_to articles_path
end

# features/support/paths.rb
def path_to(page_name)
  case page_name
  
  when /the homepage/
    root_path
  when /the list of articles/
    articles_path
  
  # Add more page name => path mappings here
  
  else
    raise "Can't find mapping from \"#{page_name}\" to a path."
  end
end
<!-- index.html.erb -->
<%= flash[:notice] %>
<% for article in @articles %>
  <p><%=h article.title %></p>
  <p><%=h article.content %></p>
<% end %>
<p><%= link_to "New Article", new_article_path %></p>

RSS Feed for Episode Comments 79 comments

1. Gavin Laking Mar 30, 2009 at 02:04

Hi Ryan,

I think there is a problem with the screencast today, I don't seem to be able to download or view it. Nobody else has commented, so I thought I better :-)


2. Aditya Sanghi Mar 30, 2009 at 02:09

@Gavin, Yes, me too! Initially I thought it might be a local problem or perhaps i was trying to pull it a bit too soon but it seems like something is missing on Ryan's end.

@Ryan, waiting patiently.

Cheers,
Aditya


3. Torsten Mar 30, 2009 at 02:10

same here: 503 Service Unavailable


4. Tex Mar 30, 2009 at 02:10

Yes, I cannot download screencast too...


5. Crouchy Mar 30, 2009 at 02:25

All screencasts aren't available, looks like the host is down


6. David Knorr Mar 30, 2009 at 02:43

I'm unable to download the screencasts, too. Seems that the media server is down.


7. pimpmaster Mar 30, 2009 at 02:50

No problems at all here.

Great 'cast Ryan - as a bonus you may want to add a link to Ben Mabey's presentation at MountainWest RubyConf

http://mwrc2009.confreaks.com/14-mar-2009-15-00-bdd-with-cucumber-ben-mabey.html

^^ I like how he breaks everything down


8. Tex Mar 30, 2009 at 02:52

Many thanks Ryan, you're our hero !!!


9. David Knorr Mar 30, 2009 at 03:01

The problem seems to be solved now. Great screencast, Ryan!


10. Florian Mar 30, 2009 at 03:22

Great introduction to cucumber! I already knew the framework and noticed, that you're pointing out the power of it very clear. A great starting-point for your audience.

Cheers,
Florian


11. Jay Mar 30, 2009 at 03:49

Hey Ryan,

This looks great! One question I had, is this something that can replace shoulda? I know you said to do continue doing unit tests - so is this something I use in combination with shoulda (like your use of rspec)?


12. Gavin Laking Mar 30, 2009 at 06:26

Working for me now too. :-) Wow, Cucumber is really impressive, I've been quite intimidated by the various Rails testing methodologies but this seems quite friendly.


13. Anlek Mar 30, 2009 at 07:08

Great cast!
Maybe next week you can cover Cucumber with Ajax?
Keep up the good work!


14. Ryan Bates Mar 30, 2009 at 07:25

Sorry about the download problems guys. Dreamhost has been a little flaky lately. May be time to find a mirror for when this happens.

@pimpmaster, good idea! I knew I forgot some other resource links. I'll add it.

@Jay, Cucumber is most often used with RSpec, but it isn't tied directly to any testing framework. You can definitely use Shoulda with it instead.

@Anlek, I plan to do more Cucumber testing episodes in the future, but not yet sure on the specific topics. Thanks for the suggestion.


15. Aslak Hellesøy Mar 30, 2009 at 08:20

Thanks for the gentle introduction Ryan!


16. Oleg Mar 30, 2009 at 09:38

Test::Unit::TestCase ... I mean ActiveSupport::TestCase for life!

Am I the only one who find these testing frameworks extremely annoying?


17. QuBiT Mar 30, 2009 at 12:33

Cucumber is a similar but improved way of "plain text stories"

http://blog.davidchelimsky.net/articles/2007/10/22/plain-text-stories-on-rails

and also integrates them.

In my opinion BDD (using cucumber) is the best way to test a rails application and present the result to a customer (which may not be familiar with programming).


18. Cezar Mar 30, 2009 at 14:17

Very nice screen-cast. I'm hoping to see more BDD screen-casts in the future, especially about Cucumber and RSpec.

Thanks!


19. Neil Middleton Mar 30, 2009 at 16:34

Hi Ryan,

Cracking Screencast this week - but here's a bit of an OT question - in this you're using the Rails bundle for textmate, yet you appear to have different shortcuts to the one I have - for instance forin and field are missing from mine - where did you get yours from?


20. Attila Györffy Mar 30, 2009 at 16:34

Really good introduction to BDD. I'm gonna show your screencast to the rest of my team since I already wanted to convince them about Cucumber.

Well done again,
and thanks.

Attila


21. RockyDd Mar 30, 2009 at 19:11

Great tutorial! Thanks for your work.


22. Jose Mar 31, 2009 at 05:52

Thank you for this useful and timely cast.

I'd like to see one about RSpec replacing Test::Unit.


23. Soleone Mar 31, 2009 at 08:19

That is so cool, Ryan! Just again when I needed an introduction to cucumber you come around with another screencast (was similar back with Liquid)!

All praise our mighty voice from beyond! ;)


24. Markus Arike Mar 31, 2009 at 16:55

Thanks, Ryan. Terrific intro that really helped me begin to wrap my head around Cucumber.

Seems like a great way to build a rock solid Rails apps from the bottom-up.

As always, superlative work.


25. George Apr 01, 2009 at 10:04

Very interesting. Keep it up Ryan. Can you please make a screen cast on Globalize 2.


26. Rich Apr 01, 2009 at 12:37

I've been using Watir (Web Application Testing in Watir) for a while now. I've found that when testing javascript applicaitons, the combination of Cucumber/SafariWatir/Safari4.0 is very fluid and very fast.


27. Ryan Bates Apr 01, 2009 at 13:16

@Neil, you can find those shortcuts in my textmate bundle.
http://github.com/ryanb/ryan-on-rails.tmbundle/


28. Aslak Hellesøy Apr 01, 2009 at 15:52

Markus:

Or Outside-In as we say in BDD land :-)


29. David Beckwith Apr 02, 2009 at 01:08

Well done!


30. dani Apr 02, 2009 at 01:18

Awesome! as always... thank you very much!


31. adamonrails Apr 02, 2009 at 03:07

Hey, thanks for this Ryan. It's a really great intro to Cuke.

@Rich mentioned using Cuke with Watir. I've seen a bit about this but would love to find out a little more about this and about using Cuke with Selenium.

This is a pretty neat intro to Cuke w/ Watir: http://www.vimeo.com/2871256 but it aint no Railscast!


32. Vladimir Parfinenko Apr 02, 2009 at 11:30

Awesome! I've tried cucumber and it's great!


33. Allen Apr 02, 2009 at 20:09

Hi Ryan, if using cucumber, why we are still using RSPEC?


34. Mike Apr 04, 2009 at 08:50

Great screencast. Just what I need but I'm having problems installing rpsec and rspec-rails. When I install the gems I get: Could not find main page README.rdoc

Cucumber and Webrat installed just fine though. Any ideas? I'm on leopard.


35. Tim Matheson Apr 05, 2009 at 11:13

Hi Ryan,
I was just wondering how you would go about defining a resource route using the paths case statement.

For example:

When I go to Bitstar Media's project Computer Science show page

http://pastie.org/437618

I am having alot of trouble getting this type of functionality with Cucumber. Any tips?


36. Noah Hendrix Apr 05, 2009 at 20:51

(not related)
I am having issues with a rails app that I am sure is fairly trivial. I generated a generic scaffold, implemented restful_auth per your instructions, and mucked around with the layout. Now I have discovered my named route new_todo_path doesn't work anymore it points to /todo/new which makes the url localhost/todo//todo/new or localhost//todo/new because I had set todo as the root controller in my routing file. Any ideas? All other routes work fine. Another strange thing is if I manually go to localhost/todo/new it creates a record without me even submitting the form.


37. Noah Hendrix Apr 05, 2009 at 20:56

Disregard the part about the model I think I have fixed that


38. wijet Apr 06, 2009 at 13:18

Hey, great episode! I definitely have to try BDD with cucumber.


39. Kenneth Wagner Apr 07, 2009 at 21:02

I have setup Cucumber as outlined in the Railscast. Cucumber on a Windows Vista System console does not show correct (ansi escape coded) colors.

How to setup Windows Vista Home Premium w Rails 2.3, cucumber, rspec,
rspec-rails, webrat for proper Cucumber console colors.

Superb intro to Cucumber. Many thanks.

Ken Wagner


40. Florian Apr 08, 2009 at 02:56

@Kenneth:
Try to use this tutorial to install Cygwin as a Unix-like terminal:

Cygwin Info: http://en.wikipedia.org/wiki/Cygwin

Tutorial for rails: http://phaseshiftllc.com/archives/2008/10/02/setting-up-rails-on-windows-with-cygwin

- Florian


41. Francis Fish Apr 08, 2009 at 06:13

I just installed all this and the default webrat file was missing the go to method (amongst others). Also didn't have the routes file, either.

I lifted them from your git repository and then webrat started complaining that it wasn't configured properly and needed

Webrat.configure do |config|
  config.mode = :rails
end

Suspect the gems have moved on but as usual the documentation doesn't tell you.


42. iGEL Apr 10, 2009 at 03:26

Ryan, you are a really mean guy, you know? I wanted to use the easter weekend to get forward with my app, but I guess, now I'm gonna play around with cukes all day long. ;)


43. fnl Apr 14, 2009 at 13:30

missing in your code listing:

# config/routes.rb
map.resource :articles


44. fnl Apr 14, 2009 at 13:31

ups... should be:

# config/routes.rb
map.resources :articles


45. fnl Apr 14, 2009 at 13:38

OK, repeated you tutorial. Just started with Rails and wanted to check out a good TDD scheme for Ruby and found Cucumber & RSpec. The homepages are nice for experts, but not a single step-by-step guide for getting it to work for newbs. Especially the rake commands are given NOWHERE and as a newb the problem with tables reported by Cucumber (if you don't know you need to clone) and the official Cucumber Wiki example actually using a table in the feature got me going crazy... Your railscast on this should become part of the official documentation! Thanks loads, man!!!


46. Eric Apr 16, 2009 at 07:37

Great screencast as always!!

I am new to BDD and am trying to figure out when it is that one would use rspec in this process? After the Cucumber features pass? Would rspec just get used for unit testing at that point?

Thanks again!
Eric


47. QuBiT Apr 23, 2009 at 12:32

Hi,

for all windows (Netbeans) developers:

I've written a Module for Netbeans which allows "Syntax Highlighting" and more for .feature files.

So if you are interested in it, you can find it on the Netbeans homepage:

http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=17939

or on my homepage:

http://members.chello.at/server/modules.html


48. Jaime Iniesta Apr 27, 2009 at 09:57

Thanks once again for your railscasts, Ryan!

I'm now learning cucumber and found your explanations very concise and helpful. :)


49. Jean-Marc May 17, 2009 at 13:37

Thanks for the great Railscasts...

I had trouble installing webrat on ubuntu until I installed the required packages described on this page http://wiki.rubyonrails.org/testing/webrat#installation

-- Jean-Marc


50. Steve Rowley May 17, 2009 at 20:00

If you are new and following along at home with a newer version of Cucumber (I've got 0.3.5), the -n switch for the "cucumber features" command will generate an error. Apparently -n refers to a different option in later versions, and the current equivalent is "--no-source".

Love these testing screencasts, Ryan! They are the only comprehensive source I've seen that walks through using all of the tools together to write tests for and code for a vaguely realistic Rails app, without assuming I am already a testing wizard.


51. rap Jun 10, 2009 at 04:32

Thank you for bringing us this information, I truly did appreciate the good work.


52. Alvin Jun 10, 2009 at 21:47

That 's so cool.


53. pulkit Jun 25, 2009 at 06:56

Hey Guys,
Don't panic if you are getting below error when you tried to run "cucumber features -n":

missing argument: -n (OptionParser::MissingArgument)

You are getting this error because cucumber version is now updated.

Try to run "cucumber features" and it will work fine!!!


54. Bilal Jun 25, 2009 at 10:22

Hi,
First of all thanks for making this tutorial, someone told me it was a good one to learn from.

Here's my current problem

~/www/blog$ cucumber features -n
bash: cucumber: command not found

Any idea what i can do ? i haven't been able to find a fix on Google ( perhaps i was searching improperly )


55. Bilal Aslam Jun 25, 2009 at 14:06

I have posted a version for people with Ubuntu 9.04 to give this a shot. I hope you guys dont mind. I did give credit at the top of the post.

http://aslambilal.blogspot.com/2009/06/beginning-with-cucumber-for-ubuntu-904.html


56. Resell Rights Jul 31, 2009 at 19:02

Great introduction to cucumber! I already knew the framework and noticed, that you're pointing out the power of it very clear. A great starting-point for your audience.


57. Single Moms Jul 31, 2009 at 19:03

I'm unable to download the screencasts, too. Seems that the media server is down.


58. gigi Aug 16, 2009 at 16:59

I cannot download screencast too...


59. Rob Aug 26, 2009 at 04:03

I've created a custom formatter that generates better HTML output for use with your cucumber projects and textmate


60. Rob Aug 26, 2009 at 04:04

I've created a custom formatter that generates better HTML output for use with your cucumber projects and textmate

http://github.com/raldred/cucumber_textmate/tree/master


61. trfuntr Oct 03, 2009 at 07:11

thank youuu


62. myo Oct 03, 2009 at 07:11

thanks


63. pornolar Oct 03, 2009 at 07:22

thanksssssssssssssss


64. Zeybekler Oct 03, 2009 at 07:34

Thanks for explaining.


65. sivas sohbet Oct 05, 2009 at 01:47

Problems such as over, thanks ryan


66. Laser lipo Oct 18, 2009 at 01:22

This is a spam farm.


67. dj ateş dinle Nov 07, 2009 at 10:06

These are excellent tips on creating a buzzing community. I particularly like No. 11 which mentions counting the number of shared experiences rather than the users. Very true indeed. This is exactly how a good community grows.

I'm new here. Love it already.

Reply


68. abercrombie fitch outlet Nov 17, 2009 at 17:36

VERY NICE!!


69. dpcldi@yahoo.com Nov 18, 2009 at 01:07

www.b5333.cn


70. Robertico Gonzalez Nov 23, 2009 at 17:04

I just installed Cucumber and Ruby on Rails on a Fedora 12 virtual machine. I am having the following issue complaining about jopenssl. Any ideas what could be wrong? Thanks

[root@bldr-vcm25 blog]# rake gems:install RAILS_ENV=test --trace
(in /home/aavella/Documents/blog)
** Invoke gems:install (first_time)
** Invoke gems:base (first_time)
** Execute gems:base
** Invoke environment (first_time)
** Execute environment
rake aborted!
no such file to load -- jopenssl
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/initializer.rb:271:in `require_frameworks'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/initializer.rb:134:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/initializer.rb:113:in `send'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/initializer.rb:113:in `run'
/home/aavella/Documents/blog/config/environment.rb:9
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:156:in `require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:521:in `new_constants_in'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:156:in `require'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/misc.rake:4
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/local/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/gems.rake:17
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/local/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:in `invoke_with_call_chain'
/usr/local/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/local/bin/rake:19:in `load'
/usr/local/bin/rake:19
[root@bldr-vcm25 blog]#


71. sygdomme Nov 26, 2009 at 01:42

Jeg er ny til BDD og jeg forsøger at finde ud af, når det er, at man ville bruge rspec i denne proces?


72. gty Nov 26, 2009 at 17:27

Just started with Rails and wanted to check out a good TDD scheme for Ruby and found Cucumber & RSpec.


74. season Dec 07, 2009 at 00:23

Your article is very good.I like it very much.
www.eshooes.com


74. Paul Dec 15, 2009 at 08:29

cucumber, never heard of it.


75. JPB Dec 21, 2009 at 15:23

Thanks for the great screencast, Ryan.
Note:
From Cucumber 0.4.5 and onwards you will also need to install the 'cucumber-rails' gem, or else 'script/generate cucumber' will fail.


76. Susan Dec 23, 2009 at 00:26

瑞安非常感谢,你是我们的英雄!<a href= http://www.crazypurchase.com>中国批发“/为一大<a href= http://www.crazypurchase.com>cell phones</a>截屏”谢谢!


77. cpbo Dec 27, 2009 at 05:44

thanks for the tutorial. its great


78. fractur3d Jan 04, 2010 at 13:32

FYI, as of cucumber-0.4.2 you'll need to use cucumber-rails otherwise the generators wont work.

sudo gem install cucumber-rails
config.gem "cucumber-rails"


79. wholesale nike shoes Jan 13, 2010 at 22:57

A very good article, I will always come in.


80. wholesale scarves Jan 13, 2010 at 22:57

Such a good article, caught my sympathy!
-

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source