Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using just t (not I18n.t) in specs #304

Merged
merged 1 commit into from
Mar 20, 2014

Conversation

gabebw
Copy link
Contributor

@gabebw gabebw commented Mar 20, 2014

No description provided.

@@ -88,12 +88,14 @@ Suspenders also comes with:
* [Fast-failing factories][fast]
* A [low database connection pool limit][pool]
* [Safe binstubs][binstub]
* [Ability to use t() in specs][i18n]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've also included l().

@jferris
Copy link
Member

jferris commented Mar 20, 2014

This looks good to me.

@calebhearth
Copy link
Contributor

Yup. One documentation suggestion.

@gabebw gabebw merged commit 7ea5176 into master Mar 20, 2014
@gabebw gabebw deleted the gbw-include-abstract-controller-translation branch March 20, 2014 17:12
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include AbstractController::Translation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be rather ActionView::Helpers::TranslationHelper? I'm not sure on the differences between the two

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither. I've used this on 2 projects so far and I haven't run into any problems. The TranslationHelper might be the wrapper that allows you to do t(".hello") (note the leading dot), which we wouldn't use in specs.

reshleman added a commit to reshleman/suspenders that referenced this pull request Dec 18, 2015
Suspenders [currently mixes-in][1] `AbstractController::Translation` in
specs [so that the `t()` helper method is available][2]. However, this
helper method produces unintuitive failure/error messages in certain
cases when used in specs:

1. Passing a missing translation key to `t()` in specs produces an RSpec
failure indicating that the text "translation missing" was not found,
instead of an exception indicating the key itself is missing from the
translations.

  ```
  Failure/Error: expect(page).to have_content t("invalid_key")
        expected to find text "translation missing: en.invalid_key" in
       "All text on the page."
  ```

  Additionally, when using this kind of expectation against `page` in a
feature spec, RSpec outputs the entire text of the page, which may fill
the terminal window and cause the actual failure message to scroll out
of view.

2. Attempting to use a relative translation key (i.e., `t(".foo")`)
within in a spec results in an unintuitive `NameError`:

  ```
  Failure/Error: expect(page).to have_content t(".invalid_key")
  NameError:
        undefined local variable or method `controller_path' for
        #<RSpec::ExampleGroups::IndexPage:0x00>
  ```

To improve these error messages, this commit mixes-in
[`ActionView::Helpers::TranslationHelper`][3] in specs instead of
[`AbstractController::Translation`][4].

These modules both define `t()` and `l()` helpers, and ultimately
delegate to `I18n.t()` and `I18n.l()`, respectively; however,
`ActionView::Helpers::TranslationHelper` produces more intuitive error
messages in the above cases.

Specifically:

1. `t()` now respects `ActionView::Base.raise_on_missing_translations`
(set to `true` in the test environment), so missing translation keys in
specs will raise an exception and the failure message will not output
the extraneous page content:

  ```
  Failure/Error: expect(page).to have_content t("invalid_key")
  I18n::MissingTranslationData:
        translation missing: en.invalid_key
  ```

2. Erroneously using a relative key now raises a clearer exception:

  ```
  Failure/Error: expect(page).to have_content t(".invalid_key")
  RuntimeError:
        Cannot use t(".invalid_key") shortcut because path is not
        available
  ```

[1]: thoughtbot#304
[2]: https://robots.thoughtbot.com/foolproof-i18n-setup-in-rails#time-39-s-a-wasting
[3]: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/translation_helper.rb
[4]: https://github.com/rails/rails/blob/master/actionpack/lib/abstract_controller/translation.rb
Web-Go-To added a commit to Web-Go-To/rails_suspenders that referenced this pull request Mar 23, 2023
Suspenders [currently mixes-in][1] `AbstractController::Translation` in
specs [so that the `t()` helper method is available][2]. However, this
helper method produces unintuitive failure/error messages in certain
cases when used in specs:

1. Passing a missing translation key to `t()` in specs produces an RSpec
failure indicating that the text "translation missing" was not found,
instead of an exception indicating the key itself is missing from the
translations.

  ```
  Failure/Error: expect(page).to have_content t("invalid_key")
        expected to find text "translation missing: en.invalid_key" in
       "All text on the page."
  ```

  Additionally, when using this kind of expectation against `page` in a
feature spec, RSpec outputs the entire text of the page, which may fill
the terminal window and cause the actual failure message to scroll out
of view.

2. Attempting to use a relative translation key (i.e., `t(".foo")`)
within in a spec results in an unintuitive `NameError`:

  ```
  Failure/Error: expect(page).to have_content t(".invalid_key")
  NameError:
        undefined local variable or method `controller_path' for
        #<RSpec::ExampleGroups::IndexPage:0x00>
  ```

To improve these error messages, this commit mixes-in
[`ActionView::Helpers::TranslationHelper`][3] in specs instead of
[`AbstractController::Translation`][4].

These modules both define `t()` and `l()` helpers, and ultimately
delegate to `I18n.t()` and `I18n.l()`, respectively; however,
`ActionView::Helpers::TranslationHelper` produces more intuitive error
messages in the above cases.

Specifically:

1. `t()` now respects `ActionView::Base.raise_on_missing_translations`
(set to `true` in the test environment), so missing translation keys in
specs will raise an exception and the failure message will not output
the extraneous page content:

  ```
  Failure/Error: expect(page).to have_content t("invalid_key")
  I18n::MissingTranslationData:
        translation missing: en.invalid_key
  ```

2. Erroneously using a relative key now raises a clearer exception:

  ```
  Failure/Error: expect(page).to have_content t(".invalid_key")
  RuntimeError:
        Cannot use t(".invalid_key") shortcut because path is not
        available
  ```

[1]: thoughtbot/suspenders#304
[2]: https://robots.thoughtbot.com/foolproof-i18n-setup-in-rails#time-39-s-a-wasting
[3]: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/translation_helper.rb
[4]: https://github.com/rails/rails/blob/master/actionpack/lib/abstract_controller/translation.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants