#159 More on Cucumber
There is a lot more to Cucumber than I showed in an earlier episode. See how to refactor complex scenarios in this episode.
- Download:
- source codeProject Files in Zip (115 KB)
- mp4Full Size H.264 Video (29.6 MB)
- m4vSmaller H.264 Video (19.8 MB)
- webmFull Size VP8 Video (55 MB)
- ogvFull Size Theora Video (38.8 MB)
Resources
- Episode 155: Beginning with Cucumber
- Episode 156: Webrat
- Episode 158: Factories not Fixtures
- Cucumber
- Cucumber Textmate Bundle
- Cucumber Presentation
- Full Episode Source Code
bash
sudo rake gems:install RAILS_ENV=test cucumber features -n -t focus cucumber features -n -t \~focus
feature
@focus Feature: Manage Users In order to manage user details As a security enthusiast I want to edit user profiles only when authorized Scenario Outline: Show or hide edit profile link Given the following user records | username | password | admin | | bob | secret | false | | admin | secret | true | Given I am logged in as "<login>" with password "secret" When I visit profile for "<profile>" Then I should <action> Examples: | login | profile | action | | admin | bob | see "Edit Profile" | | bob | bob | see "Edit Profile" | | | bob | not see "Edit Profile" | | bob | admin | not see "Edit Profile" |
config/environments/test.rb
config.gem "cucumber", :lib => false, :version => ">=0.3.0"
features/support/env.rb
require "#{Rails.root}/spec/factories"
features/support/paths.rb
World(NavigationHelpers)
features/step_definitions/user_steps.rb
Given /^the following (.+) records?$/ do |factory, table| table.hashes.each do |hash| Factory(factory, hash) end end Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |username, password| unless username.blank? visit login_url fill_in "Username", :with => username fill_in "Password", :with => password click_button "Log in" end end When /^I visit profile for "([^\"]*)"$/ do |username| user = User.find_by_username!(username) visit user_url(user) end
rhtml
<% if current_user && current_user.admin? || current_user == @user %> <p><%= link_to "Edit Profile", edit_user_path(@user) %></p> <% end %>