How do I test routes? #27

Closed
rachel-carvalho opened this Issue Jun 10, 2013 · 16 comments

Projects

None yet

5 participants

@rachel-carvalho

Whenever I declare my routes adding localization

  localized do
    resources :municipios
  end

my generated controller tests all fail

  1) Error:
test_should_get_index(MunicipiosControllerTest):
ActionController::RoutingError: No route matches {:controller=>"municipios"}
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:544:in `raise_routing_error'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:540:in `rescue in generate'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:532:in `generate'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:573:in `generate'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:569:in `generate_extras'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:565:in `extra_keys'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:153:in `assign_parameters'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:465:in `process'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:49:in `process'
    /home/rachel/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:392:in `get'
    test/functional/municipios_controller_test.rb:9:in `block in <class:MunicipiosControllerTest>'

This happens even if I only use english as config.i18n.available_locales and config.i18n.default_locale. But whenever I remove the localized block, all tests pass.

Is there a way I can test those routes with get :action?

@astjohn
astjohn commented Jun 20, 2013

This might be related to my issue #26.

There is some information out there for a few fixes:
http://www.ruby-forum.com/topic/3448797

However, it didn't help my particular situation out that much.

@rachel-carvalho

I ended up using rails' default way to translate routes (seeing I was only translating actions anyway):

scope path_names: { new: I18n.t('routes.new'), edit: I18n.t('routes.edit') } do
  resources :municipios
end

So, I'm not actually using route_translator anymore.

Might be useful to you too, @astjohn.

@GSI
GSI commented Oct 6, 2013

+1

Gem seems to work in production, but in test any _path or _url calls without explicit language code cause NoMethodError: undefined method 'foo_path' ...

Any known solutions so far?

@astjohn
astjohn commented Oct 7, 2013

@GSI, you could try giving my fork a try on the "fix_rendered_paths" branch.
https://github.com/astjohn/route_translator/tree/fix_rendered_paths

@GSI
GSI commented Oct 7, 2013

@astjohn, thanks for the hint. I'd already tried your fork, but the same tests fail with the same error messages like with the original gem.

I suppose it's working for you. Any idea how your setup might differ?
The only "specialities" in my routes are some constraints, scopes and the devise_for declaration.

@astjohn
astjohn commented Oct 8, 2013

@GSI, hm... not sure. Can you please double check that you're using the branch instead of master on my fork? Your Gemfile should look something like like the following:

gem 'route_translator', git: 'https://github.com/astjohn/route_translator.git', branch: 'fix_rendered_paths'

Then of course bundle install and re-start your server.

@GSI
GSI commented Oct 8, 2013

@astjohn that's exactly what I have in my Gemfile. I also tried adding the exact ref just to be sure.

I note that you write "re-start your server" - to clarify: production works fine, but with rake test whenever calls are made to some ..._path or ..._url (e.g. user_url), there's an NME.

@astjohn
astjohn commented Oct 9, 2013

@GSI,

I'm not sure. I use rspec most of the time, but in my controller tests, assertions on redirects using _path seem to be working well. There's nothing special about our setup, and we have some pretty complicated routes. The translated and un-translated routes all seem to be working well. I'm not sure what else it can be. Sorry I'm all out of insights.

@GSI
GSI commented Nov 28, 2013

@enriclluelles, any idea on this?

@GSI GSI pushed a commit to GSI/route_translator_reproduce_issue-27 that referenced this issue Jan 26, 2014
GSI2014 reproduced error with 'route_translator' as outlined in enriclluelles… f91ebdd
@GSI
GSI commented Jan 26, 2014

I published a Rails 4.0.2 project that reproduces the error:
https://github.com/GSI/route_translator_reproduce_issue-27

When you check it out and run "rake test", you should see the same error as shown in the README.

When removing the line "localized do" (and it's corresponding "end") from config/routes.rb, the "rake test" command succeeds.

@GSI
GSI commented Jan 29, 2014

There's a bounty for solving this on oDesk now (https://www.odesk.com/jobs/~014df4f3bce62bb002).

@astjohn
astjohn commented Jan 29, 2014

@GSI,

With the branch I stated above, I have no issues. Here is an snipped from one of our controller tests:

require 'spec_helper'

describe Partner::AppointmentsController do
  let(:country) {mock_model(Country).as_null_object}

  before do
    setup_partner_controller_environment
  end

  describe "GET index" do
    before do
      @employee.stub_chain(:locations, :only) {[]}
    end

    describe "when the company doesn't have any locations yet" do
      before do
        @company.stub_chain(:locations, :empty?) {true}
      end
      it "redirects to the location_needed page" do
        get :index
        response.should redirect_to location_needed_partner_appointments_path
      end
    end
# etc..
end

That works fine and I can't think of how unit test might be different than rspec in this scenario.

Sorry I can't be of much more help, but it works perfectly fine for me in all environments, including test. (In your example app, you don't have the branch specified for my repo/fix.)

@GSI
GSI commented Jan 29, 2014

Thank you for the update @astjohn. I've checked out your branch in the test project (https://github.com/GSI/route_translator_reproduce_issue-27). The problem persists.

Maybe it's Devise-related ... does your project use Devise?

@astjohn
astjohn commented Jan 29, 2014

@GSI, yes. We use Devise so unfortunately, I don't think that's it. However, I would go ahead and add a test model, controller, and routes for something separate to be sure. If you have time, try adding and setting up rspec as well to see if there are any differences there (also a long shot).

@GSI
GSI commented Feb 9, 2014

Thanks to @mohamagdy for solving this issue within the PR #45

@gi-lunaweb
Collaborator

PR #45 have been merged into master.

All tests are green but it will help if you can test this new route_translator in your apps before we release it.

@kirillplatonov kirillplatonov added a commit to kirillplatonov/route_translator that referenced this issue Sep 15, 2015
@kirillplatonov kirillplatonov Fix regression
PR #45 fixed issues #27 and #71. But related code was removed from
master at some point. Bring it back.
3bcf230
@kirillplatonov kirillplatonov added a commit to kirillplatonov/route_translator that referenced this issue Sep 15, 2015
@kirillplatonov kirillplatonov Fix regression
PR #45 fixed issues #27 and #71. But related code was removed from
master at some point. Bring it back.
d9d6c88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment