I'm making use of Services in my app, which is not one of the "standard" app components.
Let's say I have a spec test as follows
require "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
end
Just testing that the string returned matches a specific translation string.
However this throws an error because the helper t()
isn't available.
I could refer it to explicitly as I18n.t()
, but for my own curiosity, how do I include the correct module to have the luxury of calling the shorthand form?
Thanks!
You should be able to add it to the RSpec configuration using;
RSpec.configure do |config|
config.include AbstractController::Translation
end
Which means you can then just use t()
instead of I18n.t()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With