邮编为I18n的Rspec

ale*_*lex 7 rspec ruby-on-rails actionmailer internationalization ruby-on-rails-3

我不明白如何使用rspec和国际化进行测试.例如,在我做的请求测试中

I18n.available_locales.each do |locale|
  visit users_path(locale: locale)
  #...
end
Run Code Online (Sandbox Code Playgroud)

它工作得很好:每个语言环境测试都是正确的.

但在邮寄者中,这个技巧不起作用.

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
Run Code Online (Sandbox Code Playgroud)

它运行几次(我有多少语言环境),但每次只生成默认语言环境的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
...
Run Code Online (Sandbox Code Playgroud)

视图/ 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' %>
Run Code Online (Sandbox Code Playgroud)

我再说一遍 - 它适用于所有语言环境.我只有关于rspec测试的问题.

eug*_*gen 1

您可能需要指定区域设置,如下所示:

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

您还可以尝试在方法中的调用I18n.locale = user.locale之前添加。mailregistration_confirmation