user2490003
user2490003

Reputation: 11890

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!

Upvotes: 5

Views: 1046

Answers (1)

RossMc
RossMc

Reputation: 426

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

Upvotes: 9

Related Questions