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

Unlocalized route path does not work in non-feature specs #140

Open
jensb opened this issue May 7, 2016 · 12 comments
Open

Unlocalized route path does not work in non-feature specs #140

jensb opened this issue May 7, 2016 · 12 comments
Assignees

Comments

@jensb
Copy link

jensb commented May 7, 2016

What can be the reason if I have route helpers that - when called with a specific locale set - always return the default translation, not the translated route? E.g.

> app.products_path
=> "/products"                    # OK
> I18n.locale = 'de'
=> "de"
> app.products_de_path
=> "/produkte"                    # OK
> app.products_path
=> "/products"                    # should return "/produkte" since I18n.locale is 'de'! Right?

Settings:

RouteTranslator.config do |c|
  c.hide_locale = true
  c.generate_unlocalized_routes = true
end

Using Rails 4.2. Please advise where I should start to debug, I'd like to help track this and will provide a patch if I find a bug. Thank you!

@tagliala
Copy link
Collaborator

tagliala commented May 11, 2016

I think it depends on generate_unlocalized_routes

generate_unlocalized_routes Set this option to true to add translated routes without deleting original unlocalized versions. Autosets force_locale=true. Defaults to false.

I would say that products_path is the original, unlocalized version and it behaves like that.

Relevant code: https://github.com/enriclluelles/route_translator/blob/master/lib/route_translator/extensions/route_set.rb#L13

        elsif RouteTranslator.config.generate_unlocalized_routes
          add_route app, conditions, requirements, defaults, as, anchor
        end

Leaving this open for further discussion

@jensb
Copy link
Author

jensb commented May 11, 2016

Thank you - this works, but then I am missing some untranslated named route helpers in my rspec tests - strangely, only in tests. In devel mode the routes exist and work. Example:

localized do
  get 'foobar-whatever-:id' => 'foobar#whatever', as: foobar, constraints: { id: /\d+/ }
  # ...
end

With generate_unlocalized_routes = true, rake routes will result in routes like foobar_en, foobar_de but foobar is missing. However, in devel mode (and I hope in production mode too) foobar_path and foobar_url still work fine and actually respect I18n.locale! Any idea what can make this happen?

@tagliala
Copy link
Collaborator

tagliala commented May 11, 2016

Sorry but I cannot confirm

RouteTranslator.config do |config|
  config.force_locale = true
end
$ rails c test
Loading test environment (Rails 3.2.22.2)
[1] pry(main)> app.galleries_path
=> "/en/galleries"
[2] pry(main)> app.galleries_en_path
=> "/en/galleries"
[3] pry(main)> app.galleries_es_path
=> "/es/galerias"
$ rails c test
Loading test environment (Rails 4.2.5.2)
[1] pry(main)> app.press_releases_path
=> "/it/comunicati-stampa"
[2] pry(main)> app.press_releases_en_path
=> "/en/press-releases"
[3] pry(main)> app.press_releases_it_path
=> "/it/comunicati-stampa"
[4] pry(main)> 

Could you please provide a failing test case or application?

@jensb
Copy link
Author

jensb commented May 11, 2016

Will do. It only fails in my rspec tests when I use the helpers, not in the console or in the app. Very strange.

@tagliala
Copy link
Collaborator

tagliala commented May 11, 2016

I'm also using rspec tests and I got the issue.

require 'spec_helper'

describe 'Route translator' do
  it "shows localized and unlocalized routes" do
    visit press_releases_path
    visit press_releases_en_path
    visit press_releases_it_path
  end
end

Results:

$ rspec spec/route_translator_spec.rb 

Randomized with seed 60176

Route translator
  shows localized and unlocalized routes (FAILED - 1)

Failures:

  1) Route translator shows localized and unlocalized routes
     Failure/Error: visit press_releases_path

     NameError:
       undefined local variable or method `press_releases_path' for #<RSpec::ExampleGroups::RouteTranslator:0x007fd41b991550>
     # ./spec/route_translator_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.2757 seconds (files took 5.71 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/route_translator_spec.rb:4 # Route translator shows localized and unlocalized routes

Randomized with seed 60176

describe 'Route translator', type: :feature do
  it "shows localized and unlocalized routes" do
    visit press_releases_path
    visit press_releases_en_path
    visit press_releases_it_path
  end
end

Results:

$ rspec spec/route_translator_spec.rb 

Randomized with seed 11129

Route translator
  shows localized and unlocalized routes

Finished in 1.09 seconds (files took 5.66 seconds to load)
1 example, 0 failures

Please try with type: :feature, I will investigate this but help would be appreciated

@tagliala tagliala changed the title Default route helpers ignore locale, why? Unlocalized route path does not work in non-feature specs May 11, 2016
@tagliala
Copy link
Collaborator

@jensb is it possible to use url helpers outside feature specs? This question is not related to route_translator

@jensb
Copy link
Author

jensb commented May 12, 2016

In my controller specs, I include ActionView::Helpers::UrlHelper and include Rails.application.routes.url_helpers to use routing helpers. This has worked since Rails 3.0.

But the routing issue gets even stranger - in development mode rake routes does not print the nontranslated (missing) routes but they still work when the app is started from Apache Passenger. Using feature specs, translated routes work within the spec, however, I still get a ActionController::UrlGenerationError with some routing specs. I'm looking into this.

@tdtadeu
Copy link

tdtadeu commented Mar 7, 2017

any solution for this yet?

@tagliala
Copy link
Collaborator

tagliala commented Mar 7, 2017

@tdtadeu

Please try with type: :feature

#140 (comment)

@monteverdetico
Copy link

I know last post was several years ago, but in case anyone runs into this again, it seems to be that the issue arrises due to not mixing in the dynamically defined unlocalized route helpers into the base classes of the testing suite, which happens here.

To get these methods to work on specs that do not inherit from ActionController::TestCase, ActionMailer::TestCase, ActionView::TestCase, you need to include the helper_container into the base class of your specs -- in RSpec, this is RSpec::Core::ExampleGroup.

def add_helpers_to_rspec(helper_container)
  return unless Module.const_defined?('RSpec::Core::ExampleGroup')

  RSpec::Core::ExampleGroup.__send__(:include, helper_container)
end

I've currently modified this to be:

if ENV['RAILS_ENV'] == 'test'
  add_helpers_to_test_cases(helper_container)
  add_helpers_to_rspec(helper_container)
end

Hope this helps, if needed.

@tagliala tagliala self-assigned this Jul 16, 2019
@tagliala
Copy link
Collaborator

tagliala commented Jul 16, 2019

@monteverdetico thanks for the heads-up

Could you please provide a use case when you need to use visit outside a feature / system spec?

@monteverdetico
Copy link

@tagliala no problem.

In our current use case it is for testing modules that are generally mixed into a view / controller context; or, testing models that need url helper methods available to them (used in background jobs). So it's not to use visit per se, but rather to test public methods in helpers / classes that are expected to correctly build URLs.

It's possible we could treat some of the url helpers as a black box and stub them; or test the helper modules in the context of a request, but that refactor currently wasn't in the scope of work given our constraints.

Reference to the above in code: monteverdetico@1542b1e

I had a simple spec written, but felt it was slightly hacky as I had to create the modules, classes as a namespace in the test to ensure that the methods were being mixed in. Test looked very similar to the tests for the TestCase classes.

Hope this clarifies the use case a bit, but let me know if you have any other questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants