ruby-on-rails - 测试 ActionController::RoutingError: 未初始化的常量 V1::LocationsController
问题描述
在 Ruby on rails 5 上运行测试时出现未初始化的常量错误...
我正在使用命名空间 - v1 为我的 api 控制器编写测试用例,但似乎 ROR 不理解命名空间 V1 的存在。
当我运行“rails routes”时,这就是我得到的:
v1_locations GET /v1/locations(.:format) v1/locations#index
POST /v1/locations(.:format) v1/locations#create
v1_location GET /v1/locations/:id(.:format) v1/locations#show
PATCH /v1/locations/:id(.:format) v1/locations#update
PUT /v1/locations/:id(.:format) v1/locations#update
DELETE /v1/locations/:id(.:format) v1/locations#destroy
这就是我的测试文件中的内容......
class V1::LocationControllerTest < ActionDispatch::IntegrationTest
def setup()
# A bunch of stuff here
end
test "cannot retrieve a list of locations without a valid token" do
get v1_locations_url
assert_response :unauthorized
end
# More tests here...
end
这就是我的控制器的样子
class V1::LocationController < ApplicationController
# To be filled once tests run and fail
end
这是我收到的错误消息...
Error:
V1::LocationControllerTest#test_cannot_create_a_location_without_a_valid_token:
ActionController::RoutingError: uninitialized constant V1::LocationsController
test/controllers/v1/location_controller_test.rb:32:in `block in <class:LocationControllerTest>'
bin/rails test test/controllers/v1/location_controller_test.rb:31
当我向 /v1/locations 发送 GET 请求时出现页面未找到错误也很有趣
嗯......我在这里错过了什么?
谢谢,
解决方案
检查它说的错误日志ActionController::RoutingError: uninitialized constant V1::LocationsController
,而您的控制器名称是LocationController
.
请将其重命名为LocationsController
并将文件重命名为locations_controller.rb
.
推荐阅读
- angular - 离子通过 wp rest api 将图像上传到 wordpress 媒体库
- r - 在删除组内重复之前标记句子
- powerbi - 访问云 Power BI
- php - 在 PHP 中将 PDF 转换为 HTML,类似于 DocuSign
- c++ - 为什么当我改变它的位置时光源不移动?
- python - 我只能通过对话流获得一个 whatsapp 答案,而不是多个回复
- ffmpeg - 如何向 ffmpeg 命令添加压缩
- c++ - “汇编语言”的“mov”是指复制还是移动?
- python - ValueError:读取文本文件时无法将字符串转换为浮点数:'member_id'
- python - 查询数据时加载“搜索页面”会产生“404 页面未找到”页面