rspec - rails 使用命名空间时路由到错误的控制器,但只是有时
rspec - rails routes to wrong controller when using namespaces but only sometimes
请耐心等待,我在发布这篇文章之前尝试了很多方法,但我不确定您究竟需要什么才能帮助我解决这个问题。请告诉我,我会更新代码。
我遇到了 运行domly 失败规范的问题,所以我 运行 使用 rspec --bisect
命令来找出规范失败的原因。
似乎如果我按特定顺序 运行 2 个规范,规范就会失败。
我 运行ning 的命令是这样的:
be rspec './spec/features/scheduling/scheduler_spec.rb[1:3:1,1:3:2:1,1:3:2:2:1,1:3:2:2:2,1:3:3,1:3:4,1:3:5,1:3:6]' './spec/requests/api/v1/shifts_spec.rb[1:1:2]' --seed 16251
路由引擎似乎认为下面的 link 应该转到 Api::ShifsController
而它应该转到 ShiftsController
:
= link_to new_shift_path(date: date.to_date, user_id: user.try(:id)), remote: true do
= fa_icon "plus fw"
路线:
Rails.application.routes.draw do
root 'landing_page#index'
..
resources :shifts, only: [:new, :create, :destroy]
# API
namespace :api, defaults: { format: 'json' } do
scope :v1 do
resources :locations, only: [:index] do
resources :shifts, only: [:index]
end
end
end
宝石文件:
ruby '2.4.1'
..
gem 'rails', '~> 5.0.2'
gem 'capybara', '~> 2.16'
gem 'selenium-webdriver', '~> 3.7.0'
..
我得到的错误是这样的:
Failure/Error: raise ActionNotFound, "The action '#{action}' could not be found for #{self.class.name}"
AbstractController::ActionNotFound:
The action 'new' could not be found for Api::ShiftsController
注意!这仅在 运行 调整规范时才会发生。它永远不会发生在开发或生产中。嗯,我还没体验过。
如果我从路由文件中注释掉 resources :shifts, only: [:new, :create, :destroy]
行,一切都会恢复正常。
我不知道如何进行。
这可能是由于 Rails 自动加载混淆了 ShiftsController 和 API::ShiftsController(或 Shifts 和 API::Shifts 模型,如果它们存在的话),这取决于在测试中首先使用哪个,然后认为它不需要加载另一个。要解决此问题,请尝试在规范文件的顶部要求每个规范中使用的特定控制器 and/or 模型定义文件。
像这样:
# spec/features/scheduling/scheduler_spec.rb
require "#{Rails.root}/app/controllers/shifts_controller"
# spec/features/api/shifts_spec.rb
require "#{Rails.root}/app/controllers/api/shifts_controller"
请耐心等待,我在发布这篇文章之前尝试了很多方法,但我不确定您究竟需要什么才能帮助我解决这个问题。请告诉我,我会更新代码。
我遇到了 运行domly 失败规范的问题,所以我 运行 使用 rspec --bisect
命令来找出规范失败的原因。
似乎如果我按特定顺序 运行 2 个规范,规范就会失败。
我 运行ning 的命令是这样的:
be rspec './spec/features/scheduling/scheduler_spec.rb[1:3:1,1:3:2:1,1:3:2:2:1,1:3:2:2:2,1:3:3,1:3:4,1:3:5,1:3:6]' './spec/requests/api/v1/shifts_spec.rb[1:1:2]' --seed 16251
路由引擎似乎认为下面的 link 应该转到 Api::ShifsController
而它应该转到 ShiftsController
:
= link_to new_shift_path(date: date.to_date, user_id: user.try(:id)), remote: true do
= fa_icon "plus fw"
路线:
Rails.application.routes.draw do
root 'landing_page#index'
..
resources :shifts, only: [:new, :create, :destroy]
# API
namespace :api, defaults: { format: 'json' } do
scope :v1 do
resources :locations, only: [:index] do
resources :shifts, only: [:index]
end
end
end
宝石文件:
ruby '2.4.1'
..
gem 'rails', '~> 5.0.2'
gem 'capybara', '~> 2.16'
gem 'selenium-webdriver', '~> 3.7.0'
..
我得到的错误是这样的:
Failure/Error: raise ActionNotFound, "The action '#{action}' could not be found for #{self.class.name}"
AbstractController::ActionNotFound:
The action 'new' could not be found for Api::ShiftsController
注意!这仅在 运行 调整规范时才会发生。它永远不会发生在开发或生产中。嗯,我还没体验过。
如果我从路由文件中注释掉 resources :shifts, only: [:new, :create, :destroy]
行,一切都会恢复正常。
我不知道如何进行。
这可能是由于 Rails 自动加载混淆了 ShiftsController 和 API::ShiftsController(或 Shifts 和 API::Shifts 模型,如果它们存在的话),这取决于在测试中首先使用哪个,然后认为它不需要加载另一个。要解决此问题,请尝试在规范文件的顶部要求每个规范中使用的特定控制器 and/or 模型定义文件。
像这样:
# spec/features/scheduling/scheduler_spec.rb
require "#{Rails.root}/app/controllers/shifts_controller"
# spec/features/api/shifts_spec.rb
require "#{Rails.root}/app/controllers/api/shifts_controller"