Add mailer specs

Closes #316
This commit is contained in:
Andrew Haines 2012-11-13 20:28:15 +00:00
parent cfd36b45f8
commit cfbc3888e9
20 changed files with 119 additions and 94 deletions

View File

@ -1,13 +0,0 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .

View File

@ -1,13 +0,0 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require_tree .
*/

View File

@ -1,7 +1,4 @@
class ApplicationController < ActionController::Base
include LocalizedUrls
protect_from_forgery
def default_url_options(options = {})
{locale: I18n.locale, host: "www.example.com", port: nil}
end
end

View File

@ -0,0 +1,5 @@
module LocalizedUrls
def default_url_options(options = {})
{locale: I18n.locale, host: "www.example.com", port: nil}
end
end

View File

@ -1,5 +1,17 @@
class PostsController < ApplicationController
def show
fetch_post
end
def mail
fetch_post
email = PostMailer.decorated_email(@post).deliver
render text: email.body
end
private
def fetch_post
@post = Post.find(params[:id]).decorate
end
end

View File

@ -0,0 +1,3 @@
class ApplicationMailer < ActionMailer::Base
include LocalizedUrls
end

View File

@ -0,0 +1,9 @@
class PostMailer < ApplicationMailer
default from: "from@example.com"
layout "application"
def decorated_email(post)
@post = post.decorate
mail to: "to@example.com", subject: "A decorated post"
end
end

View File

@ -2,9 +2,6 @@
<html>
<head>
<title>Dummy</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

View File

@ -0,0 +1 @@
<%= render @post %>

View File

@ -0,0 +1,19 @@
<dl>
<dt>Environment:</dt>
<dd id="environment"><%= Rails.env %></dd>
<dt>Posted:</dt>
<dd id="posted_date"><%= post.posted_date %></dd>
<dt>Path with model:</dt>
<dd id="path_with_model"><%= post.path_with_model %></dd>
<dt>Path with id:</dt>
<dd id="path_with_id"><%= post.path_with_id %></dd>
<dt>URL with model:</dt>
<dd id="url_with_model"><%= post.url_with_model %></dd>
<dt>URL with id:</dt>
<dd id="url_with_id"><%= post.url_with_id %></dd>
</dl>

View File

@ -1,19 +1 @@
<dl>
<dt>Environment:</dt>
<dd id="environment"><%= Rails.env %></dd>
<dt>Posted:</dt>
<dd id="posted_date"><%= @post.posted_date %></dd>
<dt>Path with model:</dt>
<dd id="path_with_model"><%= @post.path_with_model %></dd>
<dt>Path with id:</dt>
<dd id="path_with_id"><%= @post.path_with_id %></dd>
<dt>URL with model:</dt>
<dd id="url_with_model"><%= @post.url_with_model %></dd>
<dt>URL with id:</dt>
<dd id="url_with_id"><%= @post.url_with_id %></dd>
</dl>
<%= render @post %>

View File

@ -54,6 +54,11 @@ module Dummy
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
# 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
end
end

View File

@ -13,9 +13,6 @@ Dummy::Application.configure do
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log

View File

@ -39,9 +39,6 @@ Dummy::Application.configure do
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!

View File

@ -24,11 +24,6 @@ Dummy::Application.configure do
# 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
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict

View File

@ -1,5 +1,7 @@
Dummy::Application.routes.draw do
scope "(:locale)", locale: /en|zh/ do
resources :posts, only: [:show]
resources :posts, only: [:show] do
get "mail", on: :member
end
end
end

View File

@ -0,0 +1,29 @@
require 'spec_helper'
describe PostMailer do
describe "#decorated_email" do
subject { Capybara.string(email.body.to_s) }
let(:email) { PostMailer.decorated_email(post).deliver }
let(:post) { Post.create }
it "decorates" do
subject.should have_content "Today"
end
it "can use path helpers with a model" do
subject.should have_css "#path_with_model", text: "/en/posts/#{post.id}"
end
it "can use path helpers with an id" do
subject.should have_css "#path_with_id", text: "/en/posts/#{post.id}"
end
it "can use url helpers with a model" do
subject.should have_css "#url_with_model", text: "http://www.example.com/en/posts/#{post.id}"
end
it "can use url helpers with an id" do
subject.should have_css "#url_with_id", text: "http://www.example.com/en/posts/#{post.id}"
end
end
end

View File

@ -1,48 +1,49 @@
require 'spec_helper'
require 'support/dummy_app'
shared_examples_for "a decorator in a view" do
it "works" do
# it runs in the correct environment
page.should have_css "#environment", text: environment
# it can use path helpers with a model
page.should have_css "#path_with_model", text: "/en/posts/1"
# it can use path helpers with an id
page.should have_css "#path_with_id", text: "/en/posts/1"
# it can use url helpers with a model
page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1"
# it can use url helpers with an id
page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1"
end
end
describe "integration" do
include Capybara::DSL
environment = ENV["RAILS_ENV"].to_s
raise ArgumentError, "RAILS_ENV must be development or production" unless ["development", "production"].include?(environment)
rails_env = ENV["RAILS_ENV"].to_s
raise ArgumentError, "RAILS_ENV must be development or production" unless ["development", "production"].include?(rails_env)
app = DummyApp.new(environment)
app = DummyApp.new(rails_env)
app.start_server do
describe "in #{environment}" do
describe "in #{rails_env}" do
let(:environment) { rails_env }
before { Capybara.app_host = app.url }
it "runs in the correct environment" do
visit("/posts/1")
page.should have_css "#environment", text: environment
context "in a view" do
before { visit("/posts/1") }
it_behaves_like "a decorator in a view"
end
it "decorates" do
visit("/posts/1")
page.should have_content "Today"
end
context "in a mailer" do
before { visit("/posts/1/mail") }
it "can use path helpers with a model" do
visit("/posts/1")
page.should have_css "#path_with_model", text: "/en/posts/1"
it_behaves_like "a decorator in a view"
end
it "can use path helpers with an id" do
visit("/posts/1")
page.should have_css "#path_with_id", text: "/en/posts/1"
end
it "can use url helpers with a model" do
visit("/posts/1")
page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1"
end
it "can use url helpers with an id" do
visit("/posts/1")
page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1"
end
end
end