rspec 表示自定义路径未定义
rspec says custom path are undefined
我的路线文件中有这个条目
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
如果您 运行 $ 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
其中,如果你 运行 $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。
我的路线文件中有这个条目
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
如果您 运行 $ 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
其中,如果你 运行 $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。