Permalink
Browse files

Added more scenarios to the email newsletter signup feature, includin…

…g an 'opt out' feature, as suggested at the live demo of Cucumber.
  • Loading branch information...
1 parent 9bf1d11 commit a9ad2e55969e7437bb04d4c725e89e3d50f10be5 Andrew Kasper committed Dec 1, 2010
View
4 app/controllers/users_controller.rb
@@ -1,12 +1,12 @@
#Manages users. For now, users have only an email address.
class UsersController < ApplicationController
def new
- @user ||= User.new(:email => 'foo@bar.com')
+ @user ||= User.new
end
def create
user_params = params[:user] || {}
- @user = User.new(:email => user_params[:email])
+ @user = User.new(user_params)
if @user.save
NewsletterSignupMailer.welcome_email(@user).deliver
View
1 app/mailers/newsletter_signup_mailer.rb
@@ -2,6 +2,7 @@ class NewsletterSignupMailer < ActionMailer::Base
default :from => "skate"
def welcome_email(user)
+ @user = user
mail(:to => user.email, :subject => 'Welcome to the Skate newsletter!')
end
end
View
1 app/models/user.rb
@@ -5,5 +5,6 @@ class User
property :id, Serial
property :email, String, :required => true, :format => :email_address, :unique => true
+ property :opt_out, Boolean
end
View
4 app/views/newsletter_signup_mailer/welcome_email.html.haml
@@ -1 +1,3 @@
-You are now signed up for the Skate newsletter
+You are now signed up for the Skate newsletter
+-if @user.opt_out
+ You have opted out of the begging emails
View
4 app/views/newsletter_signup_mailer/welcome_email.text.haml
@@ -1 +1,3 @@
-You are now signed up for the Skate newsletter
+You are now signed up for the Skate newsletter
+-if @user.opt_out
+ You have opted out of the begging emails
View
7 app/views/users/_form.html.haml
@@ -1,4 +1,9 @@
=form_for user do |e|
- %label email
+ =e.label :email, 'email address'
=e.text_field :email
=e.submit "Sign Up"
+ =e.label :opt_out, 'don\'t send me begging emails'
+ =e.check_box :opt_out
+ -if flash[:error].present?
+ #errors=flash[:error]
+
View
11 config/environments/development.rb
@@ -22,5 +22,16 @@
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
+
+ config.action_mailer.delivery_method = :smtp
+ # config.action_mailer.smtp_settings = {
+ # :address => "smtp.gmail.com",
+ # :port => 587,
+ # :domain => 'localhost',
+ # :user_name => 'andrew.m.kasper',
+ # :password => '<password>',
+ # :authentication => 'plain',
+ # :enable_starttls_auto => true
+ # }
end
View
34 features/manage_signing_up_for_the_newsletters.feature
@@ -3,14 +3,42 @@ Feature: Manage signing_up_for_the_newsletters
A visitor to our site
wants to give us his email address and receive the newsletter whenever it is new.
- Scenario: Seeing the newsletter signup form
+ Background:
When I visit the homepage
+
+ Scenario: Seeing the newsletter signup form
Then I should see the "email" form
Scenario: Entering an email address
- When I visit the homepage
- And I fill in "user_email" with "foo@bar.com"
+ When I fill in "user_email" with "foo@bar.com"
And I press "Sign Up"
Then I should be on the "show user" page
Then I should see "You have been signed up for the Skate newsletter"
And "foo@bar.com" should receive an email from "skate" that says "You are now signed up for the Skate newsletter"
+ And "foo@bar.com" should receive an email from "skate" that does not say "You have opted out of the begging emails"
+
+ Scenario: Entering an existing email address
+ Given a user with "email" "foo@bar.com"
+ When I fill in "user_email" with "foo@bar.com"
+ And I press "Sign Up"
+ Then I should be on the "new user" page
+ And I should see "already taken" within "#errors"
+
+ Scenario: Entering no email address
+ When I press "Sign Up"
+ Then I should be on the "new user" page
+ And I should see "must not be blank" within "#errors"
+
+ Scenario: Entering an invalid email address
+ When I fill in "user_email" with "asdf"
+ And I press "Sign Up"
+ Then I should be on the "new user" page
+ And I should see "invalid format" within "#errors"
+
+ Scenario: Opting out of begging for money email
+ When I fill in "user_email" with "foo@bar.com"
+ And I check "user_opt_out"
+ And I press "Sign Up"
+ Then I should be opted out
+ Then I should be on the "show user" page
+ And "foo@bar.com" should receive an email from "skate" that says "You have opted out of the begging emails"
View
3 features/step_definitions/model_steps.rb
@@ -0,0 +1,3 @@
+Given /^a ([^"]*) with "([^"]*)" "([^"]*)"$/ do |klass, field, value|
+ @model = Factory.create(klass.to_sym, field.to_sym => value)
+end
View
12 features/step_definitions/signing_up_for_the_newsletter_steps.rb
@@ -21,3 +21,15 @@
@email.encoded.should =~ Regexp.new(content)
# @email.html_part.should =~ Regexp.new(content)
end
+
+Then /^"([^"]*)" should receive an email from "([^"]*)" that does not say "([^"]*)"$/ do |recipient, sender, content|
+ @email = ActionMailer::Base.deliveries.last
+ @email.to.should be_include recipient
+ @email.from.should be_include sender
+ @email.encoded.should_not =~ Regexp.new(content)
+end
+
+Then /^I should be opted out$/ do
+ @user ||= User.first
+ @user.opt_out.should == true
+end
View
2 features/support/paths.rb
@@ -14,6 +14,8 @@ def path_to(page_name)
new_signing_up_for_the_newsletter_path
when /(the )?show user( page)?/
user_path @user || User.last
+ when /(the )?new user( page)?/
+ new_user_path
# Add more mappings here.
View
29 spec/controllers/users_controller_spec.rb
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
def invalid_email_expectations params={}
before :each do
@@ -16,7 +16,7 @@ def invalid_email_expectations params={}
describe UsersController do
SAMPLE_USER_DATA = {:email => 'foo@example.com'} unless defined? SAMPLE_USER_DATA
- context :new do
+ describe :new do
it 'is an action' do
controller.should be_respond_to(:new)
end
@@ -33,22 +33,27 @@ def invalid_email_expectations params={}
end
end
- context :show do
+ describe :show do
it 'is an action' do
controller.should be_respond_to(:show)
end
end
- context :create do
+ describe :create do
+ before :each do
+ @user_data = {'email' => 'foo@example.com'}
+ end
+
it 'is an action' do
controller.should be_respond_to(:create)
end
- context 'receives valid user data' do
- before :each do
- @user_data = {'email' => 'foo@example.com'}
- end
-
+ it 'respects opt_out' do
+ post :create, 'user' => @user_data.merge(:opt_out => '1')
+ User.first.opt_out.should == true
+ end
+
+ context 'when receiving valid user data' do
it 'creates a user object' do
post :create, 'user' => @user_data
User.all.count.should == 1
@@ -72,7 +77,7 @@ def invalid_email_expectations params={}
end
end
- context 'receives invalid user data' do
+ context 'when receiving invalid user data' do
describe 'such as no "user" field in the params hash' do
it 'redirects to the new user page' do
@@ -85,8 +90,8 @@ def invalid_email_expectations params={}
invalid_email_expectations :email => 'bademailaddress'
end
describe 'such as no email address' do
- invalid_email_expectations :email => ''
- invalid_email_expectations :email => nil
+ context '(empty)' do; invalid_email_expectations :email => '' ; end
+ context '(nil)' do; invalid_email_expectations :email => nil; end;
end
describe 'such as the submitted email address already existing' do
before :each do
View
3 spec/factories/user_factory.rb
@@ -0,0 +1,3 @@
+Factory.define :user do |u|
+ u.email 'foo@bar.com'
+end
View
2 spec/mailers/newsletter_signup_mailer_spec.rb
@@ -1,4 +1,4 @@
-require "spec_helper"
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
describe NewsletterSignupMailer do
describe :welcome_email do
View
9 spec/models/user_spec.rb
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
describe User do
it 'has an email' do
@@ -9,4 +9,11 @@
@user = User.new(:email => 'bademailaddress')
@user.save.should == false
end
+
+ it 'has an opt_out property' do
+ User.new.should be_respond_to(:opt_out)
+ # foo.bar?
+ # foo.should be_bar #means bar returns true
+ # foo.should_not be_bar #means bar returns false
+ end
end

0 comments on commit a9ad2e5

Please sign in to comment.