未初始化的常量请求 (NameError)

uninitialized constant Requests (NameError)

我把一些代码放在 spec/support/request_helpers.rb

module Requests
  module JsonHelpers
    def json
      @json ||= JSON.parse(response.body)
    end
  end
end

然后我将配置行添加到 spec/rails_helper.rb 以从我的规范中访问该代码。

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.include Requests::JsonHelpers, :type => :controller
end

所以当我 运行 我的规范来自 spec/controller/tasks_controller_spec.rb 我得到以下错误。

/spec/rails_helper.rb:16:in `block in <top (required)>': uninitialized constant Requests (NameError)

如何解决?

我的规格在顶部 require 'rails_helper'

您是否已添加 require 您的支持文件夹? 添加到您的 spec_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'

# with many helpers
Dir[File.dirname(__FILE__) + "/support/*.rb"].each {|f| require f }
# or only one   
# require 'support/request_helpers'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# some code here

About spec/support