ruby-on-rails - RSpec: NameError: 未初始化的常量 <MyControllerName>

标签 ruby-on-rails testing rspec controller

我是 RSpec 的新手,我只想测试我的 Controller 。我这样写我的测试:

RSpec.describe ServicesController do
  describe "GET index" do
    it "renders the index template" do
      get :index
      expect(response).to render_template("index")
    end
  end
end

但是我的 Controller 不被 RSpec 识别,我得到这个错误:

NameError: uninitialized constant ServicesController

在找到的示例中,我可以找到 require 行,这也许可以解决这个问题,但据我所知,它只涉及 lib 文件夹中的文件,而我的 Controller 路径是 app/controllers/services_controller.rb。我尝试使用路径 ../app/controllers/services_controller.rb 添加它,从 lib 文件夹出来,但这不起作用。我应该怎么办?这是非常基本的情况,但我无法在 Web 上找到任何帮助。

最佳答案

您可能会在规范的开头错过 require rails_helper:

require 'rails_helper'

关于ruby-on-rails - RSpec: NameError: 未初始化的常量 <MyControllerName>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48523994/

相关文章:

ruby-on-rails - 在评论中提及用户用户名 (@mention) - Rails

testing - 是否有与 Don Libes 的 *expect* 工具等效的脚本与网页交互?

python - 在 Pytest 断言中覆盖标准断言消息传递

ruby-on-rails - rspec 模拟对象属性赋值

ruby-on-rails - 如何使用哈希 :condition on a . 查找 nil? Rails 中的方法(has_one 关系)

mysql - Rails无法使用mysql生成模型

sql - Rails 嵌套 SQL 查询

java - TestNG 自动化框架在本地单独运行测试

ruby-on-rails - RSpec Controller 测试错误: controller does not implement method

ruby-on-rails - 我可以在 rspec get/post 上执行 "before hook"