Rspec-rails: Rspec ignores Controller Path

Created on 3 Jan 2017  路  8Comments  路  Source: rspec/rspec-rails

class Admins::UserController < Admins::BaseController

  # this was starred by @Prashant-Thorat
  def self.controller_path
      "users"
  end

  def index 
  end

  def new
  end

end

1) in case one i got URL generation error

RSpec.describe Admins::UsersController, type: :controller do
  context "get list#" do
    before do
      FactoryGirl.create_list :user, 5
      @user = create(:user, name: 'admin', email: '[email protected]', password: '87654321')
      sign_in @user
    end
    it "should get list of users" do
      get :index
      expect(response.status).to eq(200)
    end
  end
end

2) url generation error solved in case 2

RSpec.describe Admins::UsersController, type: :controller do
  context "get list#" do
    before do
      FactoryGirl.create_list :user, 5
      @user = create(:user, name: 'admin', email: '[email protected]', password: '87654321')
      sign_in @user
    end

    it "should get list of users" do
      **get :index, params: { use_route: 'admins/users/' }**
      expect(response.status).to eq(200)
    end

  end

3) While rendering new User form giving url generation error
```
Failure/Error: get :new, params: { company_id: @company.id }

 **ActionController::UrlGenerationError:
   No route matches {:action=>"new", :company_id=>1, :controller=>"users"}**
   ```it tries to find user controller outside the namespace

it "should get new user" do
# get :new, params: { use_route: 'admins/user/new' , company_id: @company.id }
get :new, params: { company_id: @company.id }
expect(response.status).to eq(200)
end
end
```

maybe-a-rails-bug

Most helpful comment

I ran into something like this. I forked and wrote a spec for it, but couldn't get it to fail... Eventually stumbled on to this StackOverflow question which solved my problem.

Seems like more of a problem with ruby namespaces and load order, not so much with rspec-rails.

All 8 comments

@Prashant-Thorat I'm not sure what you're asking here.

Could you format your code samples with ``` (three backticks). This will turn them into markdown code blocks so that we can understand what's going on. Thanks :)

@samphippen
While rendering new User giving url generation error
ActionController::UrlGenerationError:
No route matches {:action=>"new", :company_id=>1, :controller=>"users"}

```it tries to find product controller outside the namespace

@Prashant-Thorat that error is raised when you don't have a matching route, take a look at your routes using rake routes and then check for missing parameters for the route your trying to use.

i do have routes well define but my directory structure is something like

app --> controller
               --admins
                  --authorization_controller
                  --user_controller
       --> viwes
               --user
                 --new.html.erb

user view is not under admins/user so i am setting controller path on controller level by 

def self.controller_path
      "users"
  end 

it works fine in browser request but fails when rspec test

The error you've provided has nothing to do with physical file locations, it has to do with your url routes, in the browser the correct parameters are provided, in your test your missing some.

case 1) --------------------------------------------
controller code
def self.controller_path
"users"
end

def new
byebug
@user = User.new
end

spec code

require 'spec_helper'
include SignInHelper

RSpec.describe ProductAdmins::UsersController, :type => :controller do

context "when user loged in" do
  before do
    sign_in_as @user, 'product_admin'
  end
  it 'should get new product ' do
    get :new
    expect(response).to render_template :index 
  end
end

end

error : 
ActionController::UrlGenerationError:
       No route matches {:action=>"new", :controller=>"users"}

case 2)-----------------------------------------------------

 when I removed controller path from users controller action get called 

params i receive 
<ActionController::Parameters {"controller"=>"product_admins/users", "action"=>"new"} permitted: false>

I ran into something like this. I forked and wrote a spec for it, but couldn't get it to fail... Eventually stumbled on to this StackOverflow question which solved my problem.

Seems like more of a problem with ruby namespaces and load order, not so much with rspec-rails.

Closing as stale

Was this page helpful?
0 / 5 - 0 ratings