2016-07-27 5 views
1

У меня есть эта запись в моих маршрутах файлRSpec говорит пользовательский путь не определен

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

и это в моем файле спецификация

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'

все ответы, которые я нашел до сих пор, говорят, что добавление config.include Rails.application.routes.url_helpers в spec_helper.rb решит эту проблему, но это не решение моей проблемы

вот мой 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:

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

Если вы запустите $ rake routes, вы увидите, что вы получите следующий вывод дал настроенный маршрут:

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 

Итак, если вы хотели бы использовать get_info_path помощника для маршрута к одному контроллеру с тем же путем, вы можете измените маршрут, чтобы удалить опцию as:

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

W Хич, если вы запустите $rake routes, даст вам:

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

Для получения дополнительной информации об использовании scope и module, проверьте controller namespaces and routing section of the Rails routing guide.

+0

Это было именно оно, спасибо. Я не знал, что пространство имен также изменило назначение пользовательского маршрута – Jarfis

 Смежные вопросы

  • Нет связанных вопросов^_^