Rspec 请求规范:没有路由匹配
Rspec request specs: No routes match
将 rail 应用程序的请求规范(api-only)和 运行 写入请求规范 returns:
的问题
Failures:
1) Api::V1::BooksController sends a list of books
Failure/Error: get api_books_path
ActionController::RoutingError:
No route matches [GET] "/books"
起初,我虽然测试可能无法推断出目录路径,所以我确保 config.infer_spec_type_from_file_location!
在 spec/rails_helper
中
spec目录的目录结构:
spec/requests
└── api
└── v1
└── books_spec.rb
spec/requests/api/v1/books_spec.rb的内容:
require 'rails_helper'
RSpec.describe Api::V1::BooksController, :type => :request do
before { host! "api.localhost.com" }
it "sends a list of books" do
get api_books_path
expect(response).to be_successful
end
end
app/controllers的目录结构:
app/controllers
├── api
│ └── v1
│ └── books_controller.rb
├── application_controller.rb
└── concerns
config/routes.rb
的内容:
require 'api_versions'
Rails.application.routes.draw do
namespace :api, defaults: {format: :json}, constraints: { subdomain: ['api', 'staging.api'] }, path: "/" do
scope module: :v1, constraints: ApiVersions.new(version: 1, default: true) do
resources :books, only: [:index]
end
end
end
我似乎无法弄清楚我错过了什么;感谢任何反馈。
睡个好觉确实有帮助。我醒了;重新审视了这个问题并意识到了罪魁祸首。
- 主要问题是在
spec/requests/api/v1/books_spec.rb
中主机被设置为 api.localhost.com
(localhost.com;真的吗?)而它应该是 api.localhost:3000
。
- 第二个问题是忘记在测试中为
config.action_dispatch.tld_length
设置正确的值;我在开发中做了但忘记在测试中这样做了。
将 rail 应用程序的请求规范(api-only)和 运行 写入请求规范 returns:
的问题Failures:
1) Api::V1::BooksController sends a list of books
Failure/Error: get api_books_path
ActionController::RoutingError:
No route matches [GET] "/books"
起初,我虽然测试可能无法推断出目录路径,所以我确保 config.infer_spec_type_from_file_location!
在 spec/rails_helper
spec目录的目录结构:
spec/requests
└── api
└── v1
└── books_spec.rb
spec/requests/api/v1/books_spec.rb的内容:
require 'rails_helper'
RSpec.describe Api::V1::BooksController, :type => :request do
before { host! "api.localhost.com" }
it "sends a list of books" do
get api_books_path
expect(response).to be_successful
end
end
app/controllers的目录结构:
app/controllers
├── api
│ └── v1
│ └── books_controller.rb
├── application_controller.rb
└── concerns
config/routes.rb
的内容:
require 'api_versions'
Rails.application.routes.draw do
namespace :api, defaults: {format: :json}, constraints: { subdomain: ['api', 'staging.api'] }, path: "/" do
scope module: :v1, constraints: ApiVersions.new(version: 1, default: true) do
resources :books, only: [:index]
end
end
end
我似乎无法弄清楚我错过了什么;感谢任何反馈。
睡个好觉确实有帮助。我醒了;重新审视了这个问题并意识到了罪魁祸首。
- 主要问题是在
spec/requests/api/v1/books_spec.rb
中主机被设置为api.localhost.com
(localhost.com;真的吗?)而它应该是api.localhost:3000
。 - 第二个问题是忘记在测试中为
config.action_dispatch.tld_length
设置正确的值;我在开发中做了但忘记在测试中这样做了。