Skip to content

Commit 4108d40

Browse files
committed
Adding rspec and cucumber
1 parent 1386c3e commit 4108d40

File tree

12 files changed

+428
-0
lines changed

12 files changed

+428
-0
lines changed
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
2+
3+
# Commonly used webrat steps
4+
# http://github.com/brynary/webrat
5+
6+
Given /^I am on (.+)$/ do |page_name|
7+
visit path_to(page_name)
8+
end
9+
10+
When /^I go to (.+)$/ do |page_name|
11+
visit path_to(page_name)
12+
end
13+
14+
When /^I press "([^\"]*)"$/ do |button|
15+
click_button(button)
16+
end
17+
18+
When /^I follow "([^\"]*)"$/ do |link|
19+
click_link(link)
20+
end
21+
22+
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
23+
fill_in(field, :with => value)
24+
end
25+
26+
When /^I select "([^\"]*)" from "([^\"]*)"$/ do |value, field|
27+
select(value, :from => field)
28+
end
29+
30+
# Use this step in conjunction with Rail's datetime_select helper. For example:
31+
# When I select "December 25, 2008 10:00" as the date and time
32+
When /^I select "([^\"]*)" as the date and time$/ do |time|
33+
select_datetime(time)
34+
end
35+
36+
# Use this step when using multiple datetime_select helpers on a page or
37+
# you want to specify which datetime to select. Given the following view:
38+
# <%= f.label :preferred %><br />
39+
# <%= f.datetime_select :preferred %>
40+
# <%= f.label :alternative %><br />
41+
# <%= f.datetime_select :alternative %>
42+
# The following steps would fill out the form:
43+
# When I select "November 23, 2004 11:20" as the "Preferred" date and time
44+
# And I select "November 25, 2004 10:30" as the "Alternative" date and time
45+
When /^I select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, datetime_label|
46+
select_datetime(datetime, :from => datetime_label)
47+
end
48+
49+
# Use this step in conjunction with Rail's time_select helper. For example:
50+
# When I select "2:20PM" as the time
51+
# Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat
52+
# will convert the 2:20PM to 14:20 and then select it.
53+
When /^I select "([^\"]*)" as the time$/ do |time|
54+
select_time(time)
55+
end
56+
57+
# Use this step when using multiple time_select helpers on a page or you want to
58+
# specify the name of the time on the form. For example:
59+
# When I select "7:30AM" as the "Gym" time
60+
When /^I select "([^\"]*)" as the "([^\"]*)" time$/ do |time, time_label|
61+
select_time(time, :from => time_label)
62+
end
63+
64+
# Use this step in conjunction with Rail's date_select helper. For example:
65+
# When I select "February 20, 1981" as the date
66+
When /^I select "([^\"]*)" as the date$/ do |date|
67+
select_date(date)
68+
end
69+
70+
# Use this step when using multiple date_select helpers on one page or
71+
# you want to specify the name of the date on the form. For example:
72+
# When I select "April 26, 1982" as the "Date of Birth" date
73+
When /^I select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label|
74+
select_date(date, :from => date_label)
75+
end
76+
77+
When /^I check "([^\"]*)"$/ do |field|
78+
check(field)
79+
end
80+
81+
When /^I uncheck "([^\"]*)"$/ do |field|
82+
uncheck(field)
83+
end
84+
85+
When /^I choose "([^\"]*)"$/ do |field|
86+
choose(field)
87+
end
88+
89+
When /^I attach the file at "([^\"]*)" to "([^\"]*)"$/ do |path, field|
90+
attach_file(field, path)
91+
end
92+
93+
Then /^I should see "([^\"]*)"$/ do |text|
94+
response.should contain(text)
95+
end
96+
97+
Then /^I should not see "([^\"]*)"$/ do |text|
98+
response.should_not contain(text)
99+
end
100+
101+
Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
102+
field_labeled(field).value.should =~ /#{value}/
103+
end
104+
105+
Then /^the "([^\"]*)" field should not contain "([^\"]*)"$/ do |field, value|
106+
field_labeled(field).value.should_not =~ /#{value}/
107+
end
108+
109+
Then /^the "([^\"]*)" checkbox should be checked$/ do |label|
110+
field_labeled(label).should be_checked
111+
end
112+
113+
Then /^I should be on (.+)$/ do |page_name|
114+
URI.parse(current_url).path.should == path_to(page_name)
115+
end

features/support/env.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Sets up the Rails environment for Cucumber
2+
ENV["RAILS_ENV"] ||= "test"
3+
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
4+
require 'cucumber/rails/world'
5+
require 'cucumber/formatter/unicode' # Comment out this line if you don't want Cucumber Unicode support
6+
Cucumber::Rails.use_transactional_fixtures
7+
Cucumber::Rails.bypass_rescue # Comment out this line if you want Rails own error handling
8+
# (e.g. rescue_action_in_public / rescue_responses / rescue_from)
9+
10+
require 'webrat'
11+
12+
Webrat.configure do |config|
13+
config.mode = :rails
14+
end
15+
16+
require 'cucumber/rails/rspec'
17+
require 'webrat/core/matchers'

features/support/paths.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module NavigationHelpers
2+
# Maps a name to a path. Used by the
3+
#
4+
# When /^I go to (.+)$/ do |page_name|
5+
#
6+
# step definition in webrat_steps.rb
7+
#
8+
def path_to(page_name)
9+
case page_name
10+
11+
when /the homepage/
12+
'/'
13+
14+
# Add more mappings here.
15+
# Here is a more fancy example:
16+
#
17+
# when /^(.*)'s profile page$/i
18+
# user_profile_path(User.find_by_login($1))
19+
20+
else
21+
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
22+
"Now, go and add a mapping in #{__FILE__}"
23+
end
24+
end
25+
end
26+
27+
World(NavigationHelpers)

lib/tasks/cucumber.rake

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$LOAD_PATH.unshift(RAILS_ROOT + '/vendor/plugins/cucumber/lib') if File.directory?(RAILS_ROOT + '/vendor/plugins/cucumber/lib')
2+
3+
begin
4+
require 'cucumber/rake/task'
5+
6+
Cucumber::Rake::Task.new(:features) do |t|
7+
t.fork = true
8+
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'pretty')]
9+
end
10+
task :features => 'db:test:prepare'
11+
rescue LoadError
12+
desc 'Cucumber rake task not available'
13+
task :features do
14+
abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
15+
end
16+
end

lib/tasks/rspec.rake

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
2+
rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec')
3+
$LOAD_PATH.unshift("#{rspec_plugin_dir}/lib") if File.exist?(rspec_plugin_dir)
4+
5+
# Don't load rspec if running "rake gems:*"
6+
unless ARGV.any? {|a| a =~ /^gems/}
7+
8+
begin
9+
require 'spec/rake/spectask'
10+
rescue MissingSourceFile
11+
module Spec
12+
module Rake
13+
class SpecTask
14+
def initialize(name)
15+
task name do
16+
# if rspec-rails is a configured gem, this will output helpful material and exit ...
17+
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
18+
19+
# ... otherwise, do this:
20+
raise <<-MSG
21+
22+
#{"*" * 80}
23+
* You are trying to run an rspec rake task defined in
24+
* #{__FILE__},
25+
* but rspec can not be found in vendor/gems, vendor/plugins or system gems.
26+
#{"*" * 80}
27+
MSG
28+
end
29+
end
30+
end
31+
end
32+
end
33+
end
34+
35+
Rake.application.instance_variable_get('@tasks').delete('default')
36+
37+
spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
38+
task :noop do
39+
end
40+
41+
task :default => :spec
42+
task :stats => "spec:statsetup"
43+
44+
desc "Run all specs in spec directory (excluding plugin specs)"
45+
Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t|
46+
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
47+
t.spec_files = FileList['spec/**/*/*_spec.rb']
48+
end
49+
50+
namespace :spec do
51+
desc "Run all specs in spec directory with RCov (excluding plugin specs)"
52+
Spec::Rake::SpecTask.new(:rcov) do |t|
53+
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
54+
t.spec_files = FileList['spec/**/*/*_spec.rb']
55+
t.rcov = true
56+
t.rcov_opts = lambda do
57+
IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
58+
end
59+
end
60+
61+
desc "Print Specdoc for all specs (excluding plugin specs)"
62+
Spec::Rake::SpecTask.new(:doc) do |t|
63+
t.spec_opts = ["--format", "specdoc", "--dry-run"]
64+
t.spec_files = FileList['spec/**/*/*_spec.rb']
65+
end
66+
67+
desc "Print Specdoc for all plugin examples"
68+
Spec::Rake::SpecTask.new(:plugin_doc) do |t|
69+
t.spec_opts = ["--format", "specdoc", "--dry-run"]
70+
t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*')
71+
end
72+
73+
[:models, :controllers, :views, :helpers, :lib].each do |sub|
74+
desc "Run the code examples in spec/#{sub}"
75+
Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
76+
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
77+
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
78+
end
79+
end
80+
81+
desc "Run the code examples in vendor/plugins (except RSpec's own)"
82+
Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t|
83+
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
84+
t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*")
85+
end
86+
87+
namespace :plugins do
88+
desc "Runs the examples for rspec_on_rails"
89+
Spec::Rake::SpecTask.new(:rspec_on_rails) do |t|
90+
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
91+
t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*/*_spec.rb']
92+
end
93+
end
94+
95+
# Setup specs for stats
96+
task :statsetup do
97+
require 'code_statistics'
98+
::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
99+
::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
100+
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers')
101+
::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers')
102+
::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib')
103+
::STATS_DIRECTORIES << %w(Routing\ specs spec/routing) if File.exist?('spec/routing')
104+
::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
105+
::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
106+
::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
107+
::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
108+
::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
109+
::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing')
110+
end
111+
112+
namespace :db do
113+
namespace :fixtures do
114+
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z."
115+
task :load => :environment do
116+
ActiveRecord::Base.establish_connection(Rails.env)
117+
base_dir = File.join(Rails.root, 'spec', 'fixtures')
118+
fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
119+
120+
require 'active_record/fixtures'
121+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
122+
Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
123+
end
124+
end
125+
end
126+
end
127+
128+
namespace :server do
129+
daemonized_server_pid = File.expand_path("#{RAILS_ROOT}/tmp/pids/spec_server.pid")
130+
131+
desc "start spec_server."
132+
task :start do
133+
if File.exist?(daemonized_server_pid)
134+
$stderr.puts "spec_server is already running."
135+
else
136+
$stderr.puts %Q{Starting up spec_server ...}
137+
FileUtils.mkdir_p('tmp/pids') unless test ?d, 'tmp/pids'
138+
system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid)
139+
end
140+
end
141+
142+
desc "stop spec_server."
143+
task :stop do
144+
unless File.exist?(daemonized_server_pid)
145+
$stderr.puts "No server running."
146+
else
147+
$stderr.puts "Shutting down spec_server ..."
148+
system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) &&
149+
File.delete(daemonized_server_pid)
150+
end
151+
end
152+
153+
desc "restart spec_server."
154+
task :restart => [:stop, :start]
155+
156+
desc "check if spec server is running"
157+
task :status do
158+
if File.exist?(daemonized_server_pid)
159+
$stderr.puts %Q{spec_server is running (PID: #{File.read(daemonized_server_pid).gsub("\n","")})}
160+
else
161+
$stderr.puts "No server running."
162+
end
163+
end
164+
end
165+
end
166+
167+
end

script/autospec

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
3+
ENV['RSPEC'] = 'true' # allows autotest to discover rspec
4+
ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux
5+
system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) ||
6+
$stderr.puts("Unable to find autotest. Please install ZenTest or fix your PATH")

script/cucumber

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
begin
3+
load File.expand_path(File.dirname(__FILE__) + "/../vendor/plugins/cucumber/bin/cucumber")
4+
rescue LoadError => e
5+
raise unless e.to_s =~ /cucumber/
6+
require "rubygems"
7+
load File.join(Gem.bindir, "cucumber")
8+
end

script/spec

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env ruby
2+
if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)}
3+
require 'rubygems' unless ENV['NO_RUBYGEMS']
4+
else
5+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
6+
ENV["RAILS_ENV"] ||= 'test'
7+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT)
8+
end
9+
require 'spec/autorun'
10+
exit ::Spec::Runner::CommandLine.run

script/spec_server

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
3+
4+
puts "Loading Rails environment"
5+
ENV["RAILS_ENV"] ||= 'test'
6+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT)
7+
8+
require 'optparse'
9+
require 'spec/rails/spec_server'

spec/rcov.opts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--exclude "spec/*,gems/*"
2+
--rails

spec/spec.opts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--colour
2+
--format progress
3+
--loadby mtime
4+
--reverse

0 commit comments

Comments
 (0)