user1596316 user1596316 - 4 months ago 36
Ruby Question

rspec says custom path are undefined

I have this entry in my routes file

namespace :api do
namespace :v1 do
get "/get_info/:pone/:ptwo", to: "whatever#getInfo", as: "get_info"
end
end


and this in my spec file

require 'rails_helper'

RSpec.describe "controller test", type: :request do
describe "get_info" do
it "gets info" do
get get_info_path(55,55)
expect(response).to have_http_status(200)
end
end
end


When I run my spec file, it says
undefined local variable or method 'get_info_path'


all of the answers I've found so far say that adding
config.include Rails.application.routes.url_helpers
to spec_helper.rb will resolve this issue, but it isn't solving my problem

here is my spec_helper:

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

RSpec.configure do |config|
config.include Rails.application.routes.url_helpers

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups

end


and here's my rails_helper:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include ControllerSpecHelpers, :type => :controller
config.include FactoryGirl::Syntax::Methods
config.use_transactional_fixtures = true

config.infer_spec_type_from_file_location!

config.filter_rails_from_backtrace!
end

Answer

If you run $ rake routes, you'll see that you'd get the following output given your configured route:

api_v1_get_info GET /api/v1/get_info/:pone/:ptwo(.:format)  api/v1/whatever#getInfo

So, the path you would need to use in your specs is api_v1_get_info_path and not get_info_path.

Your route

namespace :api do
  namespace :v1 do
    get "/get_info/:pone/:ptwo", to: "whatever#getInfo", as: "get_info"
  end
end

is pretty much the equivalent of

scope :api, module: :api, as: :api do
  scope :v1, module: :v1, as: :v1 do
    get "/get_info/:pone/:ptwo", to: "whatever#getInfo", as: "get_info"
  end
end

So, if you would like to use a get_info_path helper to route to the same controller with the same path, you can change your route to remove the as option:

scope :api, module: :api do
  scope :v1, module: :v1 do
    get "/get_info/:pone/:ptwo", to: "whatever#getInfo", as: "get_info"
  end
end

Which, if you run $rake routes, will give you:

get_info GET /api/v1/get_info/:pone/:ptwo(.:format)  api/v1/whatever#getInfo

For more information about the usage scope and module, check out the controller namespaces and routing section of the Rails routing guide.