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

Merged
merged 1 commit into from Mar 20, 2014

Conversation

Projects
None yet
4 participants
Contributor

gabebw commented Mar 20, 2014

No description provided.

README.md
@@ -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]
@calebthompson

calebthompson Mar 20, 2014

Contributor

You've also included l().

Owner

jferris commented Mar 20, 2014

This looks good to me.

Contributor

calebthompson commented Mar 20, 2014

Yup. One documentation suggestion.

@gabebw gabebw merged commit 7ea5176 into master Mar 20, 2014

@gabebw gabebw deleted the gbw-include-abstract-controller-translation branch Mar 20, 2014

@@ -0,0 +1,3 @@
+RSpec.configure do |config|
+ config.include AbstractController::Translation
@gylaz

gylaz Mar 20, 2014

Owner

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

@gabebw

gabebw Mar 20, 2014

Contributor

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

Improve Errors for Invalid I18n Keys in Specs
Suspenders [currently][1] mixes-in `AbstractController::Translation` in
specs [so that the `t()` helper method is available][2]. However, this
helper method produces to two unintuitive failure 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 a failure 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."

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`:

> 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 produce a clearer RSpec failure, without the extraneous page
content output:

> I18n::MissingTranslationData:
>       translation missing: en.invalid_key

2. Erroneously using a relative key also produces a clearer failure
message:

> 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

reshleman added a commit to reshleman/suspenders that referenced this pull request Dec 18, 2015

Improve Errors for Invalid I18n Keys in Specs
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment