我有以下 Controller :
- /controllers/api/base_controller.rb
- /controllers/api/v1/articles_controller.rb
当为文章运行我的 Controller 测试时,我收到错误 RuntimeError: @controller is nil: make sure you set it in your test's setup method
。我的测试文件开始于:
require 'test_helper'
class ApiArticlesControllerTest < ActionController::TestCase
etc...
class
行应该是什么?我尝试了几个版本但没有成功。
最佳答案
您的 Controller 名称是有范围的,因此您应该将其称为 Api::V1::ArticlesController
Scoped 表示它的名称在另一个模块的范围内(“下”),在您的情况下有两个 Api
和 V1
.
将您的测试更改为 class Api::V1::ArticlesControllerTest < ActionController::TestCase
应该可以解决您的问题。
编辑
如果上面不行你也可以做以下
class ApiArticlesControllerTest < ActionController::TestCase
tests Api::V1::ArticlesController
关于ruby-on-rails - Controller 测试 : RuntimeError: @controller is nil. 如果 Controller 在子文件夹中,如何引用 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856715/