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)