-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002_rspec_and_cucumber.rails
235 lines (192 loc) · 6.69 KB
/
002_rspec_and_cucumber.rails
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Feature & Spec Rails template
#
# Builds on top of:
# - 001_core.rails
#
# Inherited setup:
# - Rails application directory structure initiliazed with git as the SCM system
# - Standard "ignored files" are added to '.gitignore' (.log, database.yml, etc)
# - Saved standard "empty" directories you want in your app
# - A base database.yml file set up to talk to Sqlite3
# - Removes unnecessary files...except index.html (README, et cetera)
# - A single "Initial commit" commit in your repository history
#
# Added functionality/features:
# - RSpec, rspec-rails, webrat, and cucumber gems
# - Builds the gems
# - Removes the test directory
# - App initialized for both rspec and cucumber
# - ie: './script/generate rspec' (& cucumber) have been run
#
# Helper Methods
def replace_default_test_environment_config
filename = "config/environments/test.rb"
run "rm -f #{filename}"
file filename,
%q{# Settings specified here will take precedence over those in config/environment.rb
config.gem 'webrat', :lib => false
config.gem 'rspec-rails', :lib => false, :version => '>= 1.2.2'
config.gem 'rspec', :lib => false, :version => '>= 1.2.2'
config.gem 'cucumber', :version => ">= 0.2.0"
config.gem 'ZenTest'
config.gem 'builder'
config.gem 'diff-lcs', :lib => 'diff/lcs'
config.gem 'nokogiri'
config.gem 'treetop'
config.gem 'term-ansicolor', :lib => 'term/ansicolor'
config.gem 'thoughtbot-factory_girl', :lib => 'factory_girl', :source => 'http://gems.github.com'
config.gem 'bmabey-email_spec', :lib => 'email_spec', :source => 'http://gems.github.com'
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_view.cache_template_loading = true
# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
}
end
def add_gem_for_testing_email
file "features/support/env.rb",
%q{# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails/world'
require 'cucumber/formatters/unicode' # Comment out this line if you don't want Cucumber Unicode support
# This adds support for email matchers in cucumber
# Read more here: http://github.com/bmabey/email-spec/tree/master
require 'email_spec/cucumber'
Cucumber::Rails.use_transactional_fixtures
require 'webrat'
Webrat.configure do |config|
config.mode = :rails
end
# Comment out the next two lines if you're not using RSpec's matchers (should / should_not) in your steps.
require 'cucumber/rails/rspec'
require 'webrat/core/matchers'
}
end
def add_email_cucumber_step_definitions
file "features/step_definitions/email_steps.rb",
%q{#Commonly used email steps
#
# To add your own steps make a custom_email_steps.rb
# The provided methods are:
#
# reset_mailer
# open_last_email
# visit_in_email
# unread_emails_for
# mailbox_for
# current_email
# open_email
# read_emails_for
# find_email
module EmailHelpers
def current_email_address
"[email protected]" # Replace with your a way to find your current_email. e.g current_user.email
end
end
World {|world| world.extend EmailHelpers }
# Use this step to reset the e-mail queue within a scenario.
# This is done automatically before each scenario.
Given /^(?:a clear email queue|no emails have been sent)$/ do
reset_mailer
end
# Use this step to open the most recently sent e-mail.
When /^I open the email$/ do
open_email(current_email_address)
end
When /^I follow "(.*)" in the email$/ do |link|
visit_in_email(link)
end
Then /^I should receive (an|\d+) emails?$/ do |amount|
amount = 1 if amount == "an"
unread_emails_for(current_email_address).size.should == amount.to_i
end
Then /^"([^']*?)" should receive (\d+) emails?$/ do |address, n|
unread_emails_for(address).size.should == n.to_i
end
Then /^"([^']*?)" should have (\d+) emails?$/ do |address, n|
mailbox_for(address).size.should == n.to_i
end
Then /^"([^']*?)" should not receive an email$/ do |address|
find_email(address).should be_nil
end
Then /^I should see "(.*)" in the subject$/ do |text|
current_email.should have_subject(Regexp.new(text))
end
Then /^I should see "(.*)" in the email$/ do |text|
current_email.body.should =~ Regexp.new(text)
end
When %r{^"([^']*?)" opens? the email with subject "([^']*?)"$} do |address, subject|
open_email(address, :with_subject => subject)
end
When %r{^"([^']*?)" opens? the email with text "([^']*?)"$} do |address, text|
open_email(address, :with_text => text)
end
When /^I click the first link in the email$/ do
click_first_link_in_email
end
}
end
def update_cucumber
add_gem_for_testing_email
add_email_cucumber_step_definitions
end
# rails:rm_tmp_dirs
["./tmp/pids", "./tmp/sessions", "./tmp/sockets", "./tmp/cache"].each do |f|
run("rmdir ./#{f}")
end
# Delete unnecessary files
run "rm README"
run "rm doc/README_FOR_APP"
run "rm public/favicon.ico"
run "rm public/robots.txt"
# Remove test dir...this uses RSpec
run "rm -rf test"
# git:hold_empty_dirs
run("find . \\( -type d -empty \\) -and \\( -not -regex ./\\.git.* \\) -exec touch {}/.gitignore \\;")
# git:rails:new_app
git :init
file '.gitignore', <<-CODE
log/*.log
log/*.pid
db/*.db
db/*.sqlite3
db/schema.rb
tmp/**/*
.DS_Store
doc/api
doc/app
config/database.yml
CODE
run "cp config/database.yml config/database.yml.sample"
# Migrate to create a db/schema.rb file
rake "db:migrate"
# Add in test environment-specific gems & config
replace_default_test_environment_config
if yes?("Install gems on local system? (y/n)")
rake("gems:install", :sudo => true)
rake("gems:install", :sudo => true, :env => "test")
end
# Initialize submodules
git :submodule => "init"
generate("rspec")
generate("cucumber")
update_cucumber
git :add => "."
git :commit => "-a -m 'Initial commit'"