Reputation: 85
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!!
Upvotes: 6
Views: 7653
Reputation: 4320
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
Upvotes: 0
Reputation: 39
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.
Upvotes: 3
Reputation: 31
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
Upvotes: 2
Reputation: 2357
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
Upvotes: 9