How to set up locale in RSPEC
Asked Answered
A

4

6

I am new to RSPEC. I have wrote a RSPEC code named result_spec.rb as below:

describe '#grouped_scores' do
subject { result.grouped_scores }

let(:result) { create(:result, user: user) }

its(:keys) { is_expected.to eq [1] }
its([1]) { is_expected.to be_within(0.001).of(6) }
end

Then when I wrote the method in the model named result.rb, the sample code is as below:

def grouped_scores
  s = 0
  if score > 10 && I18n.locale == :zh then
    s = 2
  end
  return s
end

However when I test RSPEC in local, I kept getting below error:

Failures:
1) Result#grouped_scores keys should eq [1]
 Failure/Error: its(:keys) { is_expected.to eq [1] }

   expected: [1]
        got: []

   (compared using ==)
 # ./spec/models/result_spec.rb:39:in `block (3 levels) in <top (required)>'
2) Result#grouped_personality_scores [1] should be within 0.001 of 6
 Failure/Error: its([1]) { is_expected.to be_within(0.001).of(6) }
   expected 0 to be within 0.001 of 6
 # ./spec/models/result_spec.rb:40:in `block (3 levels) in <top (required)>'

So I was wondering, is it because I didn't setup the I18n.locale as "zh", therefore it didn't get the value? If so, how to assign locale in RSPEC? Or is there anything else I should know to debug the error in RSPEC?

Please help! Thanks!!

Amberjack answered 27/8, 2018 at 13:40 Comment(2)
What do you mean set up locale in rspec ? I'll share a sample of what I have understood, I could be wrong.Calvert
because in my application, I want to group the scores based on different languages. Therefore I thought in rspec, I shall test if language is setup correctly before gathering the scores.Amberjack
C
9

Testing locale

# Assuming I have a LocalesController with check_for_locale action
describe LocalesController do

  after(:each) do
    I18n.locale = :en
  end

  it "should check if the locale is zh" do
    get :check_for_locale, locale: :zh
    expect(I18n.locale).to eq(:zh)
  end

  it "should check if the locale is set to default that is english" do
    get :check_for_locale
    expect(I18n.locale).to eq(:en)
  end

end

locales_controller.rb

class LocalesController < ApplicationController

  def check_for_locale

  end

end
Calvert answered 28/8, 2018 at 7:5 Comment(2)
Thanks for the answer. In the first part you use after(:each) do I18n.locale = :en end. May I know what is the purpose of doing this?Amberjack
I18n is a global scope, you need to reset it before you use it again, else the previous value will still be stored. Hence, before running each scenario, I'm setting it to default that is enCalvert
C
3

I come across this question while looking for response to my problem

So I have an app, for which the main language is Polish, and thus it was important for me to check all the messages in this language rather than in English. In case somebody was looking for similar thing, here is how I managed that.

In tests I've added local parameter to translation to make sure that I manually always pick the proper translation:

expect(subject.errors[:password].first).to eql I18n.t('errors.password.required', locale: :pl)

But tests were working on the default :en, so I added default locale in test environment file:

# config/environments/test.rb

I18n.available_locales = [:en, :pl]
config.i18n.default_locale = :pl

Note that after changing default locale in test environment, you no longer need to pass the :pl in your tests translations. Hope it will help someone.

Client answered 3/4, 2023 at 13:39 Comment(0)
B
2

A nicer option these days is to use I18n.with_locale to wrap the lines you're interested in. You can couple this with rspec's around like this:

around(:each) do |example|
  I18n.with_locale(:zh) do
    example.run
  end
end

If you wanted to go a step further, you could probably combine it with rspec tags to tag the block (describe/context/it) with the locale and then use the value of the tag in the call to I18n.with_locale

Bradfordbradlee answered 30/12, 2022 at 12:40 Comment(0)
C
0

Just to complement Jamie's answer, here's how you would use RSpec tags with I18n.with_locale:

around(:each, :your_rspec_tag) do |example|
    I18n.with_locale(:es) do
      example.run
    end
  end

it 'your localized test', :your_rspec_tag do
....
end
Chuck answered 26/7, 2024 at 10:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.