The specifications of my Rails application are randomly due to the wrong locale

From time to time, some of the specifications of my Rails application seem random, because suddenly English is no longer the default language, but German:

   expected: "Project test customer - Project test name (Audit, Access for all, 2015-06-15).pdf"
        got: "Project test customer - Project test name (Audit, Zugang für alle, 2015-06-15).pdf"

As you can see, the part “Access for all” is suddenly “Zugang für alle”. I googled for a solution, and it seems to be I18n.localea global object, so when it changes in the specification, it is saved .

The problem is not always there, but I can play it when you specify a seed as follows: rspec --seed 51012. So it really seems that the problem is that the specification is executed before (or after) some other specification.

I have a function specification that checks if the locale can be changed, for example:

it 'offers contents in german' do
  visit root_path(locale: :de)

  expect(page).to have_content 'Willkommen'
end

I suspect this may be a problematic specification, and when it starts early, it affects other specifications.

I was hoping I could solve this problem by setting the language in the specification back to default as follows:

it 'offers contents in german' do
  visit root_path(locale: :de)

  expect(page).to have_content 'Willkommen'

  I18n.locale = :en
end

It does not work, also did not do it:

it 'offers contents in german' do
  visit root_path(locale: :de)

  expect(page).to have_content 'Willkommen'
  visit root_path(locale: :en)
end

Now I am a little ignorant. How can I debug the situation, so I definitely find the source of the problem and fix it (or at least get around it)?

Update

Using Dave's answer ( rspec --bisect), I found the problem.

# application_controller_spec.rb
describe ApplicationController do
  controller(ApplicationController) do
    def index
      render text: 'Hello World'
    end
  end

  describe 'locale parameter' do
    it 'is set to english when not available in the request' do
      get :index
      expect(I18n.locale).to eq :en
    end

    it 'can be set through the request' do
      get :index, locale: :de
      expect(I18n.locale).to eq :de
    end
  end
end

Depending on how these specifications are executed, the locale has been set to :deor :enfor the following specifications.

I fixed it with the suggested BoraMa code:

RSpec.configure do |config|
  config.after(:each) { I18n.locale = :en }
end

, RSpec/Rails ...

+4
1

, , rspec --seed 51012. , .

, rspec --bisect.

rspec --seed 51012 --bisect

RSpec (, 2), . , .

RSpec 3.3 RSpec 3.4. : https://relishapp.com/rspec/rspec-core/docs/command-line/bisect

+3

All Articles