HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Using I18n in capybara feature specs

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
capybarai18nusingspecsfeature

Problem

Here I am trying to avoid hardcoding in flash messages by using I18n. Also, I have tried to use a CSS selector for submitting the form to make the tests less brittle (I don't really care that much about what the text on the button is - more what happens when submitting).

Is this a good approach for making tests less brittle due to translation changes?

RSpec.feature "Classic Authentication" do

  let(:submit) {  find(:css, 'input[type="submit"]') }
  let(:user) { FactoryGirl.create(:user) }

  context 'Registation' do
    background do
      visit new_user_registration_path
      page.fill_in 'Email', with: 'test@example.com'
      page.fill_in 'Password', with: 'P4ssword'
      page.fill_in 'Password confirmation', with: 'P4ssword'
    end
    scenario "when I sign up with valid info" do
      submit.click
      expect(page).to have_content I18n.t('devise.registrations.signed_up')
      expect(page).to have_content I18n.t('user.sign_out')
    end
    scenario "when I sign up with invalid info" do
      page.fill_in 'Email', with: 'testasdas'
      submit.click
      expect(page).to have_content 'Email is invalid'
      expect(page).to_not have_content I18n.t('devise.registrations.signed_up')
    end
  end

  context 'Log in' do
    background do
      visit '/'
      click_link I18n.t('user.sign_in')
      page.fill_in 'Email', with: user.email
    end
    scenario 'when I sign in with valid details.' do
      page.fill_in 'Password', with: 'P4ssword'
      submit.click
      expect(page).to have_content I18n.t('devise.sessions.signed_in')
      expect(page).to have_link I18n.t('user.sign_out')
    end
    scenario 'when I sign in with an invalid password' do
      page.fill_in 'Password', with: 'H4XX0R'
      submit.click
      expect(page).to_not have_content I18n.t('devise.sessions.signed_in')
    end
  end
end

Solution

Seems OK to me.

However, it might be nice to add a "macro" to let you skip the I18n. and just use t like you do in views.

And you can add some extra checks while you're at it, since you'll i18n want it to make noise when testing.

For instance, you could add a file like spec/support/macros/i18n_macros.rb with the following:

module I18nMacros
  def t(*args)
    I18n.translate!(*args)
  end
end


Using translate! will cause i18n to raise an exception if the translation you're looking for doesn't exist.

Then include it in spec_helper.rb

config.include I18nMacros


or alternatively include it for type: :feature specs only.

And if you haven't already (it's default for newer versions of Rails), set i18n to raise exceptions in your views (the above will only raise on the stuff in your tests). Put something like this in config/environments/test.rb

# Raises error for missing translations
config.action_view.raise_on_missing_translations = true

Code Snippets

module I18nMacros
  def t(*args)
    I18n.translate!(*args)
  end
end
config.include I18nMacros
# Raises error for missing translations
config.action_view.raise_on_missing_translations = true

Context

StackExchange Code Review Q#69819, answer score: 9

Revisions (0)

No revisions yet.