rspec と国際化をテストする方法がわかりません。たとえば、私が行うリクエストテストでは
I18n.available_locales.each do |locale|
visit users_path(locale: locale)
#...
end
すべてのロケールテストが正しく動作します。
しかしメーラーでは、このトリックは機能しません。
user_mailer_spec.rb
require "spec_helper"
describe UserMailer do
I18n.available_locales.each do |locale|
let(:user) { FactoryGirl.build(:user, locale: locale.to_s) }
let(:mail_registration) { UserMailer.registration_confirmation(user) }
it "should send registration confirmation" do
puts locale.to_yaml
mail_registration.body.encoded.should include("test") # it will return error with text which allow me to ensure that for each locale the test call only :en locale email template
end
end
end
数回 (私が持っているロケールと同じ数だけ) 実行しますが、毎回デフォルトのロケールに対してのみ html を生成します。
UserMailer.registration_confirmation(@user).deliver
コントローラーから呼び出すと、正常に動作します。
user_mailer.rb
...
def registration_confirmation(user)
@user = user
mail(to: user.email, subject: t('user_mailer.registration_confirmation.subject')) do |format|
format.html { render :layout => 'mailer'}
format.text
end
end
...
ビュー/user_mailer/registration_confirmation.text.erb
<%=t '.thx' %>, <%= @user.name %>.
<%=t '.site_description' %>
<%=t '.credentials' %>:
<%=t '.email' %>: <%= @user.email %>
<%=t '.password' %>: <%= @user.password %>
<%=t '.sign_in_text' %>: <%= signin_url %>
---
<%=t 'unsubscribe' %>
繰り返しますが、すべてのロケールで問題なく動作します。rspec テストについてのみ質問があります。