简体   繁体   English

rspec-使用名称空间时,rails路由到错误的控制器,但有时

[英]rspec - rails routes to wrong controller when using namespaces but only sometimes

Please bear with me, I've tried a lot of things before posting this and I'm not sure what exactly you need to be able to help me with this. 请多多包涵,在发布此内容之前,我已经尝试了很多事情,但是我不确定您到底需要什么才能帮助我。 Just let me know and I'll update the code. 请让我知道,我将更新代码。

I have an issue with randomly failing specs so I ran the --bisect command with rspec to find out why the specs are failing. 我有一个随机失败的规格问题,所以我用rspec运行了--bisect命令,以找出规格失败的原因。 It seems that if I run 2 specs in a certain order the specs fails. 看来,如果我以一定顺序运行2个规格,则规格会失败。

The command I'm running is this: 我正在运行的命令是这样的:

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

The routing engine seem to think that the following link should go to the Api::ShifsController when it instead should go to ShiftsController : 路由引擎似乎认为以下链接应该转到Api::ShifsController ,而应该转到ShiftsController

= link_to new_shift_path(date: date.to_date, user_id: user.try(:id)), remote: true do
    = fa_icon "plus fw"

routes: 路线:

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

Gemfile: 的Gemfile:

ruby '2.4.1'
..
gem 'rails', '~> 5.0.2'
gem 'capybara', '~> 2.16'
gem 'selenium-webdriver', '~> 3.7.0'
..

The error I'm getting is this: 我得到的错误是这样的:

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

Note! 注意! This only happens sometimes when running the specs. 仅在运行规格时才发生这种情况。 It never happens in development or in production. 它永远不会发生在开发或生产中。 Well, I haven't experienced yet. 好吧,我还没有经验。 If I comment out the line resources :shifts, only: [:new, :create, :destroy] from the routes file, everything goes back to normal. 如果我注释掉路由文件中的行resources :shifts, only: [:new, :create, :destroy] ,一切都会恢复正常。 I have no idea how to proceed. 我不知道该如何进行。

This is probably caused by Rails autoloading getting confused between ShiftsController and API::ShiftsController (or Shifts and API::Shifts models if they exist) depending on which is used first in a test, and then thinking it doesn't need to load the other. 这可能是由于Rails自动加载在ShiftsController和AP​​I :: ShiftsController(或Shifts和API :: Shifts模型,如果存在)之间造成混淆的,这取决于首先在测试中使用的是哪种,然后认为它不需要加载其他。 To fix this try requiring the specific controller and/or model definition files, being used in each spec, at the top of the spec files. 要解决此问题,请尝试在每个规范中使用规范文件顶部的特定控制器和/或模型定义文件。

Like this: 像这样:

# 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM