#155 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:
- source codeProject Files in Zip (105 KB)
- mp4Full Size H.264 Video (24.8 MB)
- m4vSmaller H.264 Video (16.4 MB)
- webmFull Size VP8 Video (45.3 MB)
- ogvFull Size Theora Video (35.3 MB)
Resources
- Cucumber
- Cucumber Textmate Bundle
- Cucumber Presentation
- Episode 159: More on Cucumber
- Full Episode Source Code
bash
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
cucumber
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>