Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include the I18n helpers in Rspec Tests for Services

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!

like image 961
user2490003 Avatar asked Feb 07 '23 08:02

user2490003


1 Answers

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()

like image 163
RossMc Avatar answered Feb 24 '23 18:02

RossMc