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

Rspec ignores Controller Path #1767

Closed
Prashant-Thorat opened this issue Jan 3, 2017 · 8 comments
Closed

Rspec ignores Controller Path #1767

Prashant-Thorat opened this issue Jan 3, 2017 · 8 comments
Labels
maybe-a-rails-bug A bug that is independent of RSpec, but might be due to rails.

Comments

@Prashant-Thorat
Copy link

Prashant-Thorat commented Jan 3, 2017

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: 'xyz@gmail.com', password: '87654321')
      sign_in @user
    end
    it "should get list of users" do
      get :index
      expect(response.status).to eq(200)
    end
  end
end
  1. 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: 'xyz@gmail.com', 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
  1. 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
@fables-tales
Copy link
Member

@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 :)

@Prashant-Thorat
Copy link
Author

Prashant-Thorat commented Jan 3, 2017

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

@JonRowe
Copy link
Member

JonRowe commented Jan 6, 2017

@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.

@Prashant-Thorat
Copy link
Author

Prashant-Thorat commented Jan 7, 2017

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

@Prashant-Thorat Prashant-Thorat changed the title Rspec Controller Path ignores Rspec ignores Controller Path Jan 7, 2017
@JonRowe
Copy link
Member

JonRowe commented Jan 7, 2017

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.

@Prashant-Thorat
Copy link
Author

Prashant-Thorat commented Jan 7, 2017

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>

@se3000
Copy link

se3000 commented Mar 24, 2017

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.

@fables-tales fables-tales added the maybe-a-rails-bug A bug that is independent of RSpec, but might be due to rails. label Dec 19, 2018
@JonRowe
Copy link
Member

JonRowe commented Mar 12, 2020

Closing as stale

@JonRowe JonRowe closed this as completed Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maybe-a-rails-bug A bug that is independent of RSpec, but might be due to rails.
Projects
None yet
Development

No branches or pull requests

4 participants