在 Rspec 服务测试中包含 I18n 助手
Include the I18n helpers in Rspec Tests for Services
我正在我的应用程序中使用 Services,它不是 "standard" 应用程序组件之一。
假设我有一个规格测试如下
需要"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()
,但出于我自己的好奇心,我如何包含正确的模块才能奢侈地调用 shorthand 表单?
谢谢!
您应该可以使用以下方法将其添加到 RSpec 配置中;
RSpec.configure do |config|
config.include AbstractController::Translation
end
这意味着您可以使用 t()
而不是 I18n.t()
我正在我的应用程序中使用 Services,它不是 "standard" 应用程序组件之一。
假设我有一个规格测试如下
需要"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()
,但出于我自己的好奇心,我如何包含正确的模块才能奢侈地调用 shorthand 表单?
谢谢!
您应该可以使用以下方法将其添加到 RSpec 配置中;
RSpec.configure do |config|
config.include AbstractController::Translation
end
这意味着您可以使用 t()
而不是 I18n.t()