RSpec:NameError:未初始化的常量 <MyControllerName>

Kar*_*lak 1 testing controller rspec ruby-on-rails

我是 RSpec 的新手,我只想测试我的控制器。我这样写了我的测试:

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
Run Code Online (Sandbox Code Playgroud)

但 RSpec 无法识别我的控制器,我收到此错误:

NameError:未初始化的常量 ServicesController

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

小智 5

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

require 'rails_helper'
Run Code Online (Sandbox Code Playgroud)