Skip to content

Commit eaf7a16

Browse files
committed
added cucumber, and fixed an error where viewing a user's page while logged out broke the page
1 parent d55b655 commit eaf7a16

14 files changed

Lines changed: 400 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
log/*
22
db/*.sqlite3
3+
rerun.txt

app/views/users/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<p><%= link_to @user.link, @user.link %></p>
66
<p><%= @user.about %></p>
77
<% unless @user == current_user %>
8-
<% if current_user.watches.include? @user %>
8+
<% if current_user && current_user.watches.include?(@user) %>
99
<%= link_to "Stop watching #{@user.username}", watching_path(current_user.watchings.detect{|w| w.watches_id == @user.id}), :method => :delete %>
1010
<% else %>
1111
<%= link_to "Watch #{@user.username}", watchings_path(:watches_id => @user), :method => :post %>

config/cucumber.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<%
2+
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3+
rerun_opts = rerun.to_s.strip.empty? ? "--format progress features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4+
std_opts = "#{rerun_opts} --format rerun --out rerun.txt --strict --tags ~@wip"
5+
%>
6+
default: <%= std_opts %>
7+
wip: --tags @wip:3 --wip features

config/database.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ development:
99
# Warning: The database defined as "test" will be erased and
1010
# re-generated from your development database when you run "rake".
1111
# Do not set this db to the same as development or production.
12-
test:
12+
test: &TEST
1313
adapter: sqlite3
1414
database: db/test.sqlite3
1515
pool: 5
@@ -20,3 +20,6 @@ production:
2020
database: db/production.sqlite3
2121
pool: 5
2222
timeout: 5000
23+
24+
cucumber:
25+
<<: *TEST

config/environments/cucumber.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Edit at your own peril - it's recommended to regenerate this file
2+
# in the future when you upgrade to a newer version of Cucumber.
3+
4+
# IMPORTANT: Setting config.cache_classes to false is known to
5+
# break Cucumber's use_transactional_fixtures method.
6+
# For more information see https://rspec.lighthouseapp.com/projects/16211/tickets/165
7+
config.cache_classes = true
8+
9+
# Log error messages when you accidentally call methods on nil.
10+
config.whiny_nils = true
11+
12+
# Show full error reports and disable caching
13+
config.action_controller.consider_all_requests_local = true
14+
config.action_controller.perform_caching = false
15+
16+
# Disable request forgery protection in test environment
17+
config.action_controller.allow_forgery_protection = false
18+
19+
# Tell Action Mailer not to deliver emails to the real world.
20+
# The :test delivery method accumulates sent emails in the
21+
# ActionMailer::Base.deliveries array.
22+
config.action_mailer.delivery_method = :test
23+
24+
config.gem 'cucumber-rails', :lib => false, :version => '>=0.2.2' unless File.directory?(File.join(Rails.root, 'vendor/plugins/cucumber-rails'))
25+
config.gem 'database_cleaner', :lib => false, :version => '>=0.2.3' unless File.directory?(File.join(Rails.root, 'vendor/plugins/database_cleaner'))
26+
config.gem 'capybara', :lib => false, :version => '>=0.2.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/capybara'))
27+
config.gem 'rspec', :lib => false, :version => '>=1.2.9' unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec'))
28+
config.gem 'rspec-rails', :lib => false, :version => '>=1.2.9' unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec-rails'))
29+

db/schema.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@
5858
t.string "api_key"
5959
end
6060

61-
create_table "watches", :force => true do |t|
62-
t.integer "user_id"
63-
t.integer "watches_id"
64-
t.datetime "created_at"
65-
t.datetime "updated_at"
66-
end
67-
6861
create_table "watchings", :force => true do |t|
6962
t.integer "user_id"
7063
t.integer "watches_id"

features/hackety.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: Use hackety-hack.com
2+
3+
Scenario: Visit a user's page
4+
Given there's a user named Steve
5+
When I go to Steve's user page
6+
Then I should see "Steve"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Given /^there's a user named (\w+)$/ do |name|
2+
Factory(:user, :username => name)
3+
end
4+
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2+
# It is recommended to regenerate this file in the future when you upgrade to a
3+
# newer version of cucumber-rails. Consider adding your own code to a new file
4+
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
5+
# files.
6+
7+
8+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
9+
10+
module WithinHelpers
11+
def with_scope(locator)
12+
within(locator || 'html') { yield }
13+
end
14+
end
15+
World(WithinHelpers)
16+
17+
Given /^(?:|I )am on (.+)$/ do |page_name|
18+
visit path_to(page_name)
19+
end
20+
21+
When /^(?:|I )go to (.+)$/ do |page_name|
22+
visit path_to(page_name)
23+
end
24+
25+
When /^(?:|I )press "([^\"]*)"(?: within "([^\"]*)")?$/ do |button, selector|
26+
with_scope(selector) do
27+
click_button(button)
28+
end
29+
end
30+
31+
When /^(?:|I )follow "([^\"]*)"(?: within "([^\"]*)")?$/ do |link, selector|
32+
with_scope(selector) do
33+
click_link(link)
34+
end
35+
end
36+
37+
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, value, selector|
38+
with_scope(selector) do
39+
fill_in(field, :with => value)
40+
end
41+
end
42+
43+
When /^(?:|I )fill in "([^\"]*)" for "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
44+
with_scope(selector) do
45+
fill_in(field, :with => value)
46+
end
47+
end
48+
49+
# Use this to fill in an entire form with data from a table. Example:
50+
#
51+
# When I fill in the following:
52+
# | Account Number | 5002 |
53+
# | Expiry date | 2009-11-01 |
54+
# | Note | Nice guy |
55+
# | Wants Email? | |
56+
#
57+
# TODO: Add support for checkbox, select og option
58+
# based on naming conventions.
59+
#
60+
When /^(?:|I )fill in the following(?: within "([^\"]*)"|)?:$/ do |fields, selector|
61+
with_scope(selector) do
62+
fields.rows_hash.each do |name, value|
63+
When %{I fill in "#{name}" with "#{value}"}
64+
end
65+
end
66+
end
67+
68+
When /^(?:|I )select "([^\"]*)" from "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
69+
with_scope(selector) do
70+
select(value, :from => field)
71+
end
72+
end
73+
74+
When /^(?:|I )check "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
75+
with_scope(selector) do
76+
check(field)
77+
end
78+
end
79+
80+
When /^(?:|I )uncheck "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
81+
with_scope(selector) do
82+
uncheck(field)
83+
end
84+
end
85+
86+
When /^(?:|I )choose "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
87+
with_scope(selector) do
88+
choose(field)
89+
end
90+
end
91+
92+
When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
93+
with_scope(selector) do
94+
attach_file(field, path)
95+
end
96+
end
97+
98+
Then /^(?:|I )should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
99+
with_scope(selector) do
100+
if defined?(Spec::Rails::Matchers)
101+
page.should have_content(text)
102+
else
103+
assert page.has_content?(text)
104+
end
105+
end
106+
end
107+
108+
Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
109+
regexp = Regexp.new(regexp)
110+
with_scope(selector) do
111+
if defined?(Spec::Rails::Matchers)
112+
page.should have_xpath('//*', :text => regexp)
113+
else
114+
assert page.has_xpath?('//*', :text => regexp)
115+
end
116+
end
117+
end
118+
119+
Then /^(?:|I )should not see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
120+
with_scope(selector) do
121+
if defined?(Spec::Rails::Matchers)
122+
page.should_not have_content(text)
123+
else
124+
assert_not page.has_content?(text)
125+
end
126+
end
127+
end
128+
129+
Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
130+
regexp = Regexp.new(regexp)
131+
with_scope(selector) do
132+
if defined?(Spec::Rails::Matchers)
133+
page.should_not have_xpath('//*', :text => regexp)
134+
else
135+
assert_not page.has_xpath?('//*', :text => regexp)
136+
end
137+
end
138+
end
139+
140+
Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should contain "([^\"]*)"$/ do |field, selector, value|
141+
with_scope(selector) do
142+
if defined?(Spec::Rails::Matchers)
143+
find_field(field).value.should =~ /#{value}/
144+
else
145+
assert_match(/#{value}/, field_labeled(field).value)
146+
end
147+
end
148+
end
149+
150+
Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should not contain "([^\"]*)"$/ do |field, selector, value|
151+
with_scope(selector) do
152+
if defined?(Spec::Rails::Matchers)
153+
find_field(field).value.should_not =~ /#{value}/
154+
else
155+
assert_no_match(/#{value}/, find_field(field).value)
156+
end
157+
end
158+
end
159+
160+
Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should be checked$/ do |label, selector|
161+
with_scope(selector) do
162+
if defined?(Spec::Rails::Matchers)
163+
find_field(label)['checked'].should == 'checked'
164+
else
165+
assert field_labeled(label)['checked'] == 'checked'
166+
end
167+
end
168+
end
169+
170+
Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should not be checked$/ do |label, selector|
171+
with_scope(selector) do
172+
if defined?(Spec::Rails::Matchers)
173+
find_field(label)['checked'].should_not == 'checked'
174+
else
175+
assert field_labeled(label)['checked'] != 'checked'
176+
end
177+
end
178+
end
179+
180+
Then /^(?:|I )should be on (.+)$/ do |page_name|
181+
if defined?(Spec::Rails::Matchers)
182+
URI.parse(current_url).path.should == path_to(page_name)
183+
else
184+
assert_equal path_to(page_name), URI.parse(current_url).path
185+
end
186+
end
187+
188+
Then /^show me the page$/ do
189+
save_and_open_page
190+
end

features/support/env.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2+
# It is recommended to regenerate this file in the future when you upgrade to a
3+
# newer version of cucumber-rails. Consider adding your own code to a new file
4+
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
5+
# files.
6+
7+
ENV["RAILS_ENV"] ||= "cucumber"
8+
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
9+
10+
require 'factory_girl'
11+
require "#{Rails.root}/lib/factories.rb"
12+
13+
require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
14+
require 'cucumber/rails/rspec'
15+
require 'cucumber/rails/world'
16+
require 'cucumber/rails/active_record'
17+
require 'cucumber/web/tableish'
18+
19+
require 'capybara/rails'
20+
require 'capybara/cucumber'
21+
require 'capybara/session'
22+
require 'cucumber/rails/capybara_javascript_emulation' # Lets you click links with onclick javascript handlers without using @culerity or @javascript
23+
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
24+
# order to ease the transition to Capybara we set the default here. If you'd
25+
# prefer to use XPath just remove this line and adjust any selectors in your
26+
# steps to use the XPath syntax.
27+
Capybara.default_selector = :css
28+
29+
# If you set this to false, any error raised from within your app will bubble
30+
# up to your step definition and out to cucumber unless you catch it somewhere
31+
# on the way. You can make Rails rescue errors and render error pages on a
32+
# per-scenario basis by tagging a scenario or feature with the @allow-rescue tag.
33+
#
34+
# If you set this to true, Rails will rescue all errors and render error
35+
# pages, more or less in the same way your application would behave in the
36+
# default production environment. It's not recommended to do this for all
37+
# of your scenarios, as this makes it hard to discover errors in your application.
38+
ActionController::Base.allow_rescue = false
39+
40+
# If you set this to true, each scenario will run in a database transaction.
41+
# You can still turn off transactions on a per-scenario basis, simply tagging
42+
# a feature or scenario with the @no-txn tag. If you are using Capybara,
43+
# tagging with @culerity or @javascript will also turn transactions off.
44+
#
45+
# If you set this to false, transactions will be off for all scenarios,
46+
# regardless of whether you use @no-txn or not.
47+
#
48+
# Beware that turning transactions off will leave data in your database
49+
# after each scenario, which can lead to hard-to-debug failures in
50+
# subsequent scenarios. If you do this, we recommend you create a Before
51+
# block that will explicitly put your database in a known state.
52+
Cucumber::Rails::World.use_transactional_fixtures = true
53+
54+
# How to clean your database when transactions are turned off. See
55+
# http://github.com/bmabey/database_cleaner for more info.
56+
require 'database_cleaner'
57+
DatabaseCleaner.strategy = :truncation
58+

0 commit comments

Comments
 (0)