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

Bil*_*gan 1 rspec module ruby-on-rails helper ruby-on-rails-4

我在spec/support/request_helpers.rb中添加了一些代码

module Requests
  module JsonHelpers
    def json
      @json ||= JSON.parse(response.body)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我将配置行添加到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
Run Code Online (Sandbox Code Playgroud)

因此,当我从spec/controller/tasks_controller_spec.rb运行我的规范时,我收到以下错误.

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

怎么解决?

我的规格require 'rails_helper'在顶部.

Зел*_*ный 7

您是否已添加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
Run Code Online (Sandbox Code Playgroud)

About spec/support