Skip to content

Commit 2c8d750

Browse files
rails_apps_composer: testing framework
1 parent 8c5e0fc commit 2c8d750

File tree

10 files changed

+272
-25
lines changed

10 files changed

+272
-25
lines changed

config/application.rb

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
module KioskServer
1313
class Application < Rails::Application
14+
15+
# don't generate RSpec tests for views and helpers
16+
config.generators do |g|
17+
g.test_framework :rspec
18+
19+
20+
21+
22+
g.view_specs false
23+
g.helper_specs false
24+
end
25+
1426
# Settings in config/environments/* take precedence over those specified here.
1527
# Application configuration should go into files in config/initializers
1628
# -- all .rb files in that directory are automatically loaded.
+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Commonly used email steps
2+
#
3+
# To add your own steps make a custom_email_steps.rb
4+
# The provided methods are:
5+
#
6+
# last_email_address
7+
# reset_mailer
8+
# open_last_email
9+
# visit_in_email
10+
# unread_emails_for
11+
# mailbox_for
12+
# current_email
13+
# open_email
14+
# read_emails_for
15+
# find_email
16+
#
17+
# General form for email scenarios are:
18+
# - clear the email queue (done automatically by email_spec)
19+
# - execute steps that sends an email
20+
# - check the user received an/no/[0-9] emails
21+
# - open the email
22+
# - inspect the email contents
23+
# - interact with the email (e.g. click links)
24+
#
25+
# The Cucumber steps below are setup in this order.
26+
27+
module EmailHelpers
28+
def current_email_address
29+
# Replace with your a way to find your current email. e.g @current_user.email
30+
# last_email_address will return the last email address used by email spec to find an email.
31+
# Note that last_email_address will be reset after each Scenario.
32+
last_email_address || "[email protected]"
33+
end
34+
end
35+
36+
World(EmailHelpers)
37+
38+
#
39+
# Reset the e-mail queue within a scenario.
40+
# This is done automatically before each scenario.
41+
#
42+
43+
Given /^(?:a clear email queue|no emails have been sent)$/ do
44+
reset_mailer
45+
end
46+
47+
#
48+
# Check how many emails have been sent/received
49+
#
50+
51+
Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
52+
unread_emails_for(address).size.should == parse_email_count(amount)
53+
end
54+
55+
Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
56+
mailbox_for(address).size.should == parse_email_count(amount)
57+
end
58+
59+
Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
60+
unread_emails_for(address).select { |m| m.subject =~ Regexp.new(Regexp.escape(subject)) }.size.should == parse_email_count(amount)
61+
end
62+
63+
Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject \/([^"]*?)\/$/ do |address, amount, subject|
64+
unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
65+
end
66+
67+
Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
68+
open_email(address, :with_text => expected_body)
69+
end
70+
71+
#
72+
# Accessing emails
73+
#
74+
75+
# Opens the most recently received email
76+
When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
77+
open_email(address)
78+
end
79+
80+
When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
81+
open_email(address, :with_subject => subject)
82+
end
83+
84+
When /^(?:I|they|"([^"]*?)") opens? the email with subject \/([^"]*?)\/$/ do |address, subject|
85+
open_email(address, :with_subject => Regexp.new(subject))
86+
end
87+
88+
When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
89+
open_email(address, :with_text => text)
90+
end
91+
92+
When /^(?:I|they|"([^"]*?)") opens? the email with text \/([^"]*?)\/$/ do |address, text|
93+
open_email(address, :with_text => Regexp.new(text))
94+
end
95+
96+
#
97+
# Inspect the Email Contents
98+
#
99+
100+
Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
101+
current_email.should have_subject(text)
102+
end
103+
104+
Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
105+
current_email.should have_subject(Regexp.new(text))
106+
end
107+
108+
Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
109+
current_email.default_part_body.to_s.should include(text)
110+
end
111+
112+
Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
113+
current_email.default_part_body.to_s.should =~ Regexp.new(text)
114+
end
115+
116+
Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
117+
current_email.should be_delivered_from(text)
118+
end
119+
120+
Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
121+
current_email.should have_header(name, text)
122+
end
123+
124+
Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
125+
current_email.should have_header(name, Regexp.new(text))
126+
end
127+
128+
Then /^I should see it is a multi\-part email$/ do
129+
current_email.should be_multipart
130+
end
131+
132+
Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
133+
current_email.html_part.body.to_s.should include(text)
134+
end
135+
136+
Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
137+
current_email.text_part.body.to_s.should include(text)
138+
end
139+
140+
#
141+
# Inspect the Email Attachments
142+
#
143+
144+
Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
145+
current_email_attachments.size.should == parse_email_count(amount)
146+
end
147+
148+
Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
149+
current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
150+
end
151+
152+
Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
153+
current_email_attachments[(index.to_i - 1)].filename.should == filename
154+
end
155+
156+
Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
157+
current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
158+
end
159+
160+
Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
161+
current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
162+
end
163+
164+
Then /^all attachments should not be blank$/ do
165+
current_email_attachments.each do |attachment|
166+
attachment.read.size.should_not == 0
167+
end
168+
end
169+
170+
Then /^show me a list of email attachments$/ do
171+
EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
172+
end
173+
174+
#
175+
# Interact with Email Contents
176+
#
177+
178+
When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
179+
visit_in_email(link)
180+
end
181+
182+
When /^(?:I|they) click the first link in the email$/ do
183+
click_first_link_in_email
184+
end
185+
186+
#
187+
# Debugging
188+
# These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
189+
# Patches accepted. ;)
190+
#
191+
192+
Then /^save and open current email$/ do
193+
EmailSpec::EmailViewer::save_and_open_email(current_email)
194+
end
195+
196+
Then /^save and open all text emails$/ do
197+
EmailSpec::EmailViewer::save_and_open_all_text_emails
198+
end
199+
200+
Then /^save and open all html emails$/ do
201+
EmailSpec::EmailViewer::save_and_open_all_html_emails
202+
end
203+
204+
Then /^save and open all raw emails$/ do
205+
EmailSpec::EmailViewer::save_and_open_all_raw_emails
206+
end

spec/spec_helper.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
ENV["RAILS_ENV"] ||= 'test'
3+
require File.expand_path("../../config/environment", __FILE__)
4+
require 'rspec/rails'
5+
require 'email_spec'
6+
require 'rspec/autorun'
7+
8+
# Requires supporting ruby files with custom matchers and macros, etc,
9+
# in spec/support/ and its subdirectories.
10+
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
11+
12+
RSpec.configure do |config|
13+
config.include(EmailSpec::Helpers)
14+
config.include(EmailSpec::Matchers)
15+
# ## Mock Framework
16+
#
17+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
18+
#
19+
# config.mock_with :mocha
20+
# config.mock_with :flexmock
21+
# config.mock_with :rr
22+
23+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
24+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
25+
26+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
27+
# examples within a transaction, remove the following line or assign false
28+
# instead of true.
29+
config.use_transactional_fixtures = true
30+
31+
# If true, the base class of anonymous controllers will be inferred
32+
# automatically. This will be the default behavior in future versions of
33+
# rspec-rails.
34+
config.infer_base_class_for_anonymous_controllers = false
35+
36+
# Run specs in random order to surface order dependencies. If you find an
37+
# order dependency and want to debug it, you can fix the order by providing
38+
# the seed, which is printed after each run.
39+
# --seed 1234
40+
config.order = "random"
41+
42+
config.before(:suite) do
43+
DatabaseCleaner.strategy = :truncation
44+
end
45+
config.before(:each) do
46+
DatabaseCleaner.start
47+
end
48+
config.after(:each) do
49+
DatabaseCleaner.clean
50+
end
51+
end

spec/support/devise.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.include Devise::TestHelpers, :type => :controller
3+
end

test/fixtures/.gitkeep

Whitespace-only changes.

test/functional/.gitkeep

Whitespace-only changes.

test/integration/.gitkeep

Whitespace-only changes.

test/performance/browsing_test.rb

-12
This file was deleted.

test/test_helper.rb

-13
This file was deleted.

test/unit/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)