【问题标题】:Include the I18n helpers in Rspec Tests for Services在 Rspec 服务测试中包含 I18n 助手
【发布时间】:2016-04-04 05:46:25
【问题描述】:

我在我的应用中使用Services,它不是“标准”应用组件之一。

假设我有如下规格测试

需要“rails_helper”

# spec/services/create_user.rb
RSpec.describe CreateUser, type: :service do
  it "returns the error message" do
    error_message = Foo.new.errors
    expect(error_message).to eq(t("foo.errors.message"))
  end

结束

只是测试返回的字符串是否匹配特定的翻译字符串。

但是这会引发错误,因为帮助程序 t() 不可用。

我可以将其明确称为I18n.t(),但出于我自己的好奇心,我如何包含正确的模块才能有幸调用速记形式?

谢谢!

【问题讨论】:

    标签: ruby-on-rails rspec rails-i18n


    【解决方案1】:

    您应该能够使用以下方法将其添加到 RSpec 配置中;

    RSpec.configure do |config|
      config.include AbstractController::Translation
    end
    

    这意味着您可以只使用t() 而不是I18n.t()

    【讨论】:

    • 如何在itdescribe块之外使用?
    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    相关资源
    最近更新 更多