2016-07-27 3 views
1

을 정의되지 있다고 말했습니다RSpec에 사용자 정의 경로 내 경로 내 사양 파일에</p> <pre><code>namespace :api do namespace :v1 do get "/get_info/:pone/:ptwo", to: "whatever#getInfo", as: "get_info" end end </code></pre> <p>이 파일에 나는이 항목이

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 

, 그것은 undefined local variable or method 'get_info_path'

을 말한다

내가 찾은 모든 답변은 spec_helper.rb에 config.include Rails.application.routes.url_helpers을 추가하면이 문제가 해결되지만 내 문제는 해결되지 않는다고 말합니다.

여기

내 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 

여기 내 rails_helper : 당신이 $ rake routes을 실행하면

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 
+0

'config.include Rails.application.routes.url_helpers'을'RSpec.configure do config |'블록에 추가 했습니까? –

+0

그게 긍정 일 겁니다 – Jarfis

+0

spec_helper.rb는 어떻게 생겼습니까? –

답변

1

, 당신은 당신이 당신의 구성 경로 주어진 다음과 같은 출력을 얻을 거라고 볼 수 있습니다 :

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

따라서 사양에서 사용해야하는 경로는 api_v1_get_info_path이고가 아닌 경로입니다.. 같은 경로로 동일한 컨트롤러로 라우팅에 get_info_path 도우미를 사용하려는 경우

귀하의 경로

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

은 당신이 할 수있는, 그래서 거의

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 

하는 것과 동일

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

W 다음 as 옵션을 제거하는 경로를 변경 , 사용 scopemodule에 대한 자세한 내용은

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

controller namespaces and routing section of the Rails routing guide 체크 아웃 : HICH, 당신은 $rake routes를 실행하면, 당신을 줄 것이다.

+0

정확히 그랬습니다. 고마워요. 나는 네임 스페이스가 사용자 정의 경로 네이밍을 변경했다는 것을 몰랐습니다. – Jarfis

관련 문제