我正在使用 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()
,但出于我自己的好奇心,我如何包含正确的模块才能拥有调用速记形式的奢侈?谢谢!
最佳答案
您应该能够使用以下方法将其添加到 RSpec 配置中;
RSpec.configure do |config|
config.include AbstractController::Translation
end
这意味着您可以使用
t()
而不是 I18n.t()
关于ruby-on-rails - 在 Rspec 服务测试中包含 I18n 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395302/