enriclluelles/route_translator

Testing controller

Closed this issue · 2 comments

Hi,

I have the following controller spec

require 'spec_helper'

RSpec.describe ProfilesController, :type => :controller do
  describe "GET 'index'" do
    before do
      controller.stub(:authenticate_user!)
    end

    it "load all the profiles" do
      get :index

      expect(assigns(:profiles)).to eq Profile.all.order(:id)
    end
  end

  describe "PUT 'update'" do
    let(:profile) { profiles(:admin) }

    before do
      controller.stub(:authenticate_user!)
    end

    it "calls the ProfileUpdater service" do
      params = {"permission"=>"manage", "value"=>true, "id"=>"135138680",
                "controller"=>"profiles", "action"=>"update"}

      updater = double(:updater, status: 201)

      ProfileUpdater.should_receive(:new).with(params).and_return(updater)

      updater.should_receive(:update)

      put :update, params
    end
  end
end

and the following routes.rb

Rails.application.routes.draw do
  localized do
    resources :profiles
  end
end

When I run my controller spec I get the following error:

  1) ProfilesController GET 'index' load all the profiles
     Failure/Error: get :index
     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"profiles"}
     # ./spec/controllers/profiles_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

  2) ProfilesController PUT 'update' calls the ProfileUpdater service
     Failure/Error: put :update, params
     ActionController::UrlGenerationError:
       No route matches {:action=>"update", :controller=>"profiles", :id=>"135138680", :permission=>"manage", :value=>true}
     # ./spec/controllers/profiles_controller_spec.rb:40:in `block (3 levels) in <top (required)>'

Someone can tell me why the spec doesn't recognise the routes anymore?

Problem solved. I had to specify the locale:

    it "load all the profiles" do
      get :index, locale: 'pt-BR'

      expect(assigns(:profiles)).to eq Profile.all.order(:id)
    end

I think that the gem should use my default locale for this. Regardless of this, I solved my problem.

I'm having this problem too, I don't think that having to pass the locale in every test is the desired default behavior.

After looking around, mostly from reading #27, I get the feeling that this was corrected with #45 but was later regressed. Any ideas?