简体   繁体   中英

uninitialized constant Requests (NameError)

I put some code in spec/support/request_helpers.rb

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

Then I added config line to spec/rails_helper.rb to access that code from my spec.

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

So when I run my spec from spec/controller/tasks_controller_spec.rb I get the following error.

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

How to solve it?

My spec has require 'rails_helper' on the top.

Do you have already added require your support folder? Add to your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM