Rspec for mailers with I18n

I do not understand how to test with rspec and internationalization. For example, in query tests, I do

I18n.available_locales.each do |locale| visit users_path(locale: locale) #... end 

and it works fine: every locale test is correct.

But in email programs this trick does not work.

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 

It works several times (as many locales as I have), but each time it generates html only for the default locale.

When I call UserMailer.registration_confirmation(@user).deliver from the controller, it works fine.

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 ... 

view / 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' %> 

I repeat - it works great for all locales. My question is only about rspec tests for this.

+7
source share
2 answers

I think you might have to run a test in the describe/context block so that the it block displays your let variables:

 require "spec_helper" describe UserMailer do I18n.available_locales.each do |locale| describe "registration" do 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") end end # ... end # ... end 

As for the reasons, perhaps this StackOverflow answer in let scope might help.

Edit

Is there a problem that you have assigned a language to your user, but you are not passing it to the mail method anywhere? Perhaps this StackOverflow answer will be helpful. I hope one of the two answers there will be appropriate in your situation. Here is my simple attempt to adapt the first answer to your situation (unchecked explicitly):

user_mailer.rb

 ... def registration_confirmation(user) @user = user I18n.with_locale(user.locale) do mail(to: user.email, subject: t('user_mailer.registration_confirmation.subject')) do |format| format.html { render :layout => 'mailer' } format.text end end end ... 
+2
source

You probably need to specify the locale, as in:

mail_subscribe.body.encoded.should include(t('user_mailer.subscribe_confirmation.stay', locale: locale))

You can also try adding I18n.locale = user.locale right before calling mail in the registration_confirmation method.

+1
source

All Articles