ruby-on-rails - Controller 测试 : RuntimeError: @controller is nil. 如果 Controller 在子文件夹中,如何引用 Controller ?

标签 ruby-on-rails ruby-on-rails-4 testing

我有以下 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 表示它的名称在另一个模块的范围内(“下”),在您的情况下有两个 ApiV1 .

将您的测试更改为 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/

相关文章:

iphone - Ruby on Rails 服务器应用程序 + IOS iphone 客户端?

docker - 有没有更简单的方法来使用 Docker 图像在多平台上测试 PHP 源代码?

javascript - 循环遍历 JSON 数组并替换为 NULL 的值

testing - Gradle:自动添加对项目的测试依赖

exception - 如何在 C++/CLI NUnit 测试中使用 ExpectedException?

javascript - 在文档完全加载之前执行主干脚本

ruby-on-rails - Rails I18n 嵌套翻译键

ruby-on-rails - rails : Calling a controller method from another controller -> NoMethodError

ruby-on-rails - 如何为新模板添加路由?

html - 使用大量 HTML 渲染页面