简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM