Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails NameError:添加路由命名空间后未初始化常量ControllerName_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails NameError:添加路由命名空间后未初始化常量ControllerName

Ruby on rails NameError:添加路由命名空间后未初始化常量ControllerName,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在使用rspec-rails(3.7.1)和rails(5.1.4) 所有测试都成功运行,但在将命名空间添加到routes.rb文件后,我遇到了以下错误: An error occurred while loading ./spec/controllers/stores_controller_spec.rb. Failure/Error: RSpec.describe StoresController, type: :controller do NameError: uninitial

我正在使用
rspec-rails(3.7.1)
rails(5.1.4)

所有测试都成功运行,但在将命名空间添加到
routes.rb
文件后,我遇到了以下错误:

An error occurred while loading ./spec/controllers/stores_controller_spec.rb.
Failure/Error:
RSpec.describe StoresController, type: :controller do

NameError:
  uninitialized constant StoresController
这是我的
routes.rb
文件:

namespace 'api' do
 namespace 'v1' do
  resources :stores
 end
end
我的
存储\u控制器\u规范rb
的位置

require 'rails_helper'

RSpec.describe StoresController, type: :controller do
 let(:store) { FactoryBot.create(:store) }

 describe "GET index" do
     it 'has a 200 status code' do
         request.headers.merge!(auth_headers)
         get :index
         expect(response.status).to eq(200)
     end
 end
end

当您有控制器路由命名空间时,如

api/v1/stores
然后Rails希望控制器被命名

Api::V1::StoresController 
并位于名为

app/controllers/api/v1/stores_controller.rb

当您有控制器路由命名空间时,如

api/v1/stores
然后Rails希望控制器被命名

Api::V1::StoresController 
并位于名为

app/controllers/api/v1/stores_controller.rb

您的控制器可能是
Api::V1::storescoontroller
@JagdeepSingh我两分钟前试过这个,它成功了!请重新键入您的评论作为答案以接受它。您的控制器可能是
Api::V1::StoresController
@JagdeepSingh我在2分钟前尝试过这个,它成功了!请重新键入您的评论作为接受它的答案。您能告诉我为什么在将
Api::V1::StoresController
添加到Rspec文件
失败/错误:呈现json:@store,status::created,位置:@store nomethoderor:undefined method
store_url'for#`因为当您使用名称空间路由时,它们的方法名称也会更改。我想现在您需要使用
api\u v1\u store\u url
,而不仅仅是
store\u url
。使用命令行上的
rails-routes
查看所有路由及其名称。谢谢,伙计,但我想你误解了我的意思,所有的方法都工作得很好,没有在rspec文件中添加任何内容,但是create测试得到了这个错误,测试是
description“Post#create”do it'creates a new store'do post:create,params:{store:attributes_for(:store,vendor\u id:vendor.id,district\u id:district.id)}期望(响应)。当我从
呈现json:@store,status::created]中删除
位置:@store
时,要有_http_status(:created)end
,位置:@store
在StoresController的创建方法中,测试通过。正如我以前读到的,
location
选项用于重定向到新资源,作为处理当前请求的一部分。因此,我认为创建方法现在不需要它就可以了!是的,当然,现在我明白了
location:@store
基本上是
location:store\u url(@store)
的简短版本。但是由于名称空间的原因,现在需要使用长URL版本(Rails无法自动构建该名称空间)。只需使用:
location:api\u v1\u store\u url(@store)
您能告诉我为什么即使在将
api::v1::StoresController
添加到Rspec文件
Failure/error:render json:@store,status::created,位置:@store nomethoderor:undefined method
store_url'for#`因为当您使用名称空间路由时,它们的方法名称也会更改。我想现在您需要使用
api\u v1\u store\u url
,而不仅仅是
store\u url
。使用命令行上的
rails-routes
查看所有路由及其名称。谢谢,伙计,但我想你误解了我的意思,所有的方法都工作得很好,没有在rspec文件中添加任何内容,但是create测试得到了这个错误,测试是
description“Post#create”do it'creates a new store'do post:create,params:{store:attributes_for(:store,vendor\u id:vendor.id,district\u id:district.id)}期望(响应)。当我从
呈现json:@store,status::created]中删除
位置:@store
时,要有_http_status(:created)end
,位置:@store
在StoresController的创建方法中,测试通过。正如我以前读到的,
location
选项用于重定向到新资源,作为处理当前请求的一部分。因此,我认为创建方法现在不需要它就可以了!是的,当然,现在我明白了
location:@store
基本上是
location:store\u url(@store)
的简短版本。但是由于名称空间的原因,现在需要使用长URL版本(Rails无法自动构建该名称空间)。只需使用:
location:api\u v1\u store\u url(@store)