2016-04-04 36 views
1

我在我的應用程序中使用了Services,這不是「標準」應用程序組件之一。在Rspec服務中包含I18n幫助程序

比方說,我有一個規範的測試如下

要求「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(),但對於我自己的好奇心,我如何包含正確的模塊以便能夠稱得上速記形式?

謝謝!

回答

4

您應該可以將它添加到RSpec配置使用;

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

這意味着,你可以只使用t()代替I18n.t()