LoginSignup
2
2

More than 5 years have passed since last update.

TurnipでAction Mailer Previewsを使う

Last updated at Posted at 2015-02-26

Turnipでスクリーンショット取りたくてやってみたら出来たからメモです.

前提としてRails4.1以上でTurnipのテストができる状態になってることとします.

書き足したソース

RailsのAction Mailer Previewsはdevelopment環境でしか使えないので自前でルートを追加.

config/routes.rb
if Rails.env.test?
  get '/rails/mailers'      , to: "rails/mailers#index"
  get '/rails/mailers/*path', to: "rails/mailers#preview", as: 'mail_preview'
end

ついでにパスもspecに変更.

config/application.rb
config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"

最後に送信したメールを対象にプレビュー表示させておく.

spec/mailers/previews/test_mailer_preview.rb
class TestMailerPreview < ActionMailer::Preview

  def last
    ActionMailer::Base.deliveries.last
  end
end

stepを以下のように書いておくとfeatureで開ける。
あとはスクリーンショットのステップをコールすればいい感じ。

spec/steps/mail_steps.rb
step %(最後に送信したメールを表示) do
  url = mail_preview_path(path: 'test_mailer/last')
  visit url
end

やろうとして諦めたこと

メールプレビューからメールヘッダのチェック等をするstep

プレビューのHTMLがシンプルすぎて諦めた.

メールプレビューから本文を使ったstep

本文部分がiframeだったので萎えて諦めた.

stackoverflowに取得の仕方が書いてあったけど未検証.

within_frame(find('<css rule>')) do
  <code for dealing with iframe entries>
end
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2